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.

Resolve alias fails - "Module not found: Error: Can't resolve"

See original GitHub issue

Bug report

What is the current behavior?

Hi, I am using webpack 5 via nextjs and need to resolve an import with an alias. However it fails. Here is the config, with two different options. Both resulting in different errors. It seems Option 2 is the right direction, but it still fails. I upgraded to latest nextjs (10.2.3) and babel version (7.14.3).

webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {      
      // Option 1 fails with Cannot find module. It seems this one does still compile with mapbox-gl
      // config.resolve.alias['mapbox-gl'] = 'maplibre-gl'
      config.module.rules.push({
        resolve:{
          alias: {
            ...config.resolve.alias,
            // Option 2 via rules fails with Module not found: Error: Can't resolve 'mapbox-gl'
            'mapbox-gl': 'maplibre-gl'
          }
        }
      })
      return config
    }

If the current behavior is a bug, please provide the steps to reproduce.

I am using nextjs with react-mapbox-gl and maplibre-gl. Since react-mapbox-gl normally works with mapbox-gl the usage of the maplibre-gl fork is possible via aliases as mentioned here: https://github.com/alex3165/react-mapbox-gl/issues/924#issuecomment-755377958

Since this issue seems to be mainly related to webpack and not nextjs, I am asking the question here.

What is the expected behavior?

Within the node_module “react-mapbox-gl” all instances of mapbox-gl imports should be replaced with maplibre-gl. Is there maybe a better resolve condition than “mapbox-gl”: “maplibre-gl” to replace a module import within a node_module?

Other relevant information: webpack version: 5.? (unfortunately I do not know, which version nextjs uses, I assume one of the latest) Node.js version: 14.15.5 Operating System: Windows Additional tools: Babel 7.14.3, typescript 4.1.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
alexander-akaitcommented, Jun 5, 2021

I think Next.js uses externals for packages inside node_modules for SSR, so react-mapbox-gl is loading from node_modules directly, you can check this in stack trace:

Error: Cannot find module 'mapbox-gl'
Require stack:
- /home/user/projects/next-resolve-alias-issue/node_modules/react-mapbox-gl/lib/map.js
- /home/user/projects/next-resolve-alias-issue/node_modules/react-mapbox-gl/lib/index.js
- /home/user/projects/next-resolve-alias-issue/.next/server/pages/index.js
// ...

and aliases doesn’t work here, because webpack doesn’t touch this file (otherwise you will get an error on bundled file in .next directory).

So better ask them how do it. Anyway you can use it using yarn/npm, for npm you can do:

{
    "dependencies": {
        "mapbox-gl": "npm:maplibre-gl@^1.14.0"
    }
}

I think yarn should support this too

1reaction
dennemarkcommented, Jun 9, 2021

The solution via dependencies seems to work! I tried it with yarn within a yarn workspace environment and I did not experience any issues yet. So I see this issue as resolved. Thanks a lot again @alexander-akait , your explanation was very helpful!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack doesn't resolve properly my alias - Stack Overflow
Resolving the alias to the absolute path should do the trick: resolve: { alias: { myApp: path.resolve(__dirname, 'src'), }, extensions: ['' ...
Read more >
Module not found: Can't resolve 'module' · Issue #37 - GitHub
Can't resolve 'module' When linking a dependency locally that uses module-alias, I'm getting the error below: Module not found: Can't ...
Read more >
Resolve | webpack
Resolve. These options change how modules are resolved. Webpack provides reasonable defaults, but it is possible to change the resolving in detail.
Read more >
Webpack @ alias not working in app - only when running npm ...
Webpack @ alias not working in app - only when running npm run prod on Forge ... Module not found: Error: Can't resolve...
Read more >
Can't resolve path alias : r/reactjs - Reddit
I'm trying to use alias for paths but I can't seem to get it to work I keep getting this error even though...
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