Webpack alias does not work when alias is outside of next root
See original GitHub issueBug report
Describe the bug
Let’s say that you have your NextJS root at ./next and your React components in ./client. Now, when you configure a webpack alias to ./client/components with @components and use it in ./next/pages/Page.js - the build will fail with this error:
➜ next-webpack-issue git:(master) yarn build
yarn run v1.15.2
$ next build ./next
Creating an optimized production build ...
Failed to compile.
../app/pages/Index.js 1:21
Module parse failed: Unexpected token (1:21)
You may need an appropriate loader to handle this file type.
> export default () => <div>Hello World!</div>;
|
> Build error occurred
Error: > Build failed because of webpack errors
at Object.build [as default] (/home/jsarnowski/Workshop/next-webpack-issue/node_modules/next/dist/build/index.js:192:15)
at process._tickCallback (internal/process/next_tick.js:68:7)
error Command failed with exit code 1.
To Reproduce
Here’s an easy reproduction repo: https://github.com/sarneeh/zeit-next.js-issues-7234
Just run yarn install and yarn build after that.
Expected behavior
I expect the alias to work.
System information
- OS: Ubuntu 18.04
- Version of Next.js: 8.x and also 7.x
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Webpack doesn't resolve properly my alias - Stack Overflow
To have it work for the library's import calls as well, I had to do: webpackConfig.resolve.alias = { mobx: path.resolve(root, "node_modules" ...
Read more >Resolve | webpack
Create aliases to import or require certain modules more easily. ... If true , it will not allow extension-less files. ... foo.js') will...
Read more >How to Create a Path Alias in Webpack - Bitovi
Open your Webpack.config.js file. · If it's not already there, add a resolve property to the configuration object and make it an empty...
Read more >Webpack Aliases Keep My Code Sane - DEV Community
Webpack solves these problems by giving us the ability to use “aliases”. Aliases are a way to let webpack know where to find...
Read more >Advanced Features: Absolute Imports and Module Path Aliases
json "paths" and "baseUrl" options since Next.js 9.4. Note: jsconfig.json can be used when you don't use TypeScript. Note: you need to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I as a developer still expect the alias to work or a description in documentation of aspects of the next.js framework which break when doing otherwise ‘normal’ Webpack configuration such as module aliasing in combination with workspaces. It should at least be documented where the node ssr or client javascript breaks due to the framework’s implementation.
ok @sarneeh, I finally got this to work by adding custom
rulesin webpack config, somethings like this:I just tried this config pattern for both my own project and your issue repo and it just works fine Thank you for your webpack-merge suggestion, maybe someday i will use it if the config become more complexly