admin管理员组

文章数量:1410712

Let's assume, we do have few js libs, that installed into our bundle. And, for some reason, I need to use a library from node_modules of a library. I can do import it via

import thing from 'somelib/node_modules/thing';

And I want to do just:

import thing from 'thing';

But behind the scenes, webpack will know - the path should be 'somelib/node_modules/thing'

How can I change/override a specific import path in my webpack config file, so my node will bring me a package from the destination that I want?

Let's assume, we do have few js libs, that installed into our bundle. And, for some reason, I need to use a library from node_modules of a library. I can do import it via

import thing from 'somelib/node_modules/thing';

And I want to do just:

import thing from 'thing';

But behind the scenes, webpack will know - the path should be 'somelib/node_modules/thing'

How can I change/override a specific import path in my webpack config file, so my node will bring me a package from the destination that I want?

Share Improve this question edited Dec 4, 2018 at 15:20 Maksym Labutin 5711 gold badge8 silver badges17 bronze badges asked Dec 4, 2018 at 15:01 WebArtisanWebArtisan 4,23612 gold badges47 silver badges66 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I think you are looking for resolve.alias

https://webpack.js/configuration/resolve/#resolve-alias

In your webpack config, specify the resolve.modules

This example from the webpack documentation adds the "src" folder.

module.exports = {
  //...
  resolve: {
    modules: [path.resolve(__dirname, 'src'), 'node_modules']
  }
};

Or if you really don't want this affecting your other entry points, you could create separate webpack configs. (They can still import settings from a primary file) that allows you to set resolve.aliases and resolve.modules independently for each entry point.

module.exports = {
  //...
  resolve: [
    function() {
      regexNpmPgkName.test(xxx) {
         callback()
      }
    }

  ]
};

本文标签: javascriptWebpack change import path for a specific packageStack Overflow