question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack alias does not work when alias is outside of next root

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
Kielancommented, Nov 19, 2019

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.

3reactions
tronjscommented, May 14, 2019

ok @sarneeh, I finally got this to work by adding custom rules in webpack config, somethings like this:

// next.config.js
module.exports = withTypescript({
    ...
    distDir: '../../.next',
    webpack(config, options) {
        config.module.rules.push({
           test: /\.(ts|tsx)$/,
           include: [path.join(__dirname, 'src/lib')],
           use: [
             {
               loader: 'next-babel-loader',
               options: {
                  asyncToPromises: false,
                  cwd: path.join(__dirname, 'src/lib'),
                  isServer: false,
               },
             },
           ],
        });
      return config;
    }
    ...
});

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found