Next.js: why webpack alias doesn't work as expected in nextjs?

Created on 10 Mar 2020  路  5Comments  路  Source: vercel/next.js

Bug report

Describe the bug

webpack alias doesn't work as expected . I created an descussion here, but I think this might be a bug, because it did work a few days ago.

To Reproduce

I created a demo here

Screenshots

image

good first issue

Most helpful comment

@Exelord You just saved me a whole lot of strife. Thannnnnk ya!

All 5 comments

I did this in next config:

const config = {
  webpack(config) {
    config.resolve.alias[package.name] = path.resolve(__dirname, 'src');
    return config;
  }
};

So then I can import by app name import Input from 'my-app/components/input'

You also need to add it to tsconfig.json. You are using typescript with next.js. I got it working like this
My tsconfig.json

"baseUrl": ".",
"paths": {
     "@": ["src/"],
     "@/*": ["src/*"],
 }

My next.config.js webpack

config.resolve.alias['@'] = path.join(__dirname, 'src')

Removing one or the other breaks it.

@JazibJafri, it works, thanks a lots.

https://github.com/zeit/next.js/pull/11293
This will solve your issue better :)

@Exelord You just saved me a whole lot of strife. Thannnnnk ya!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formula349 picture formula349  路  3Comments

pie6k picture pie6k  路  3Comments

olifante picture olifante  路  3Comments

ghost picture ghost  路  3Comments

renatorib picture renatorib  路  3Comments