Blanen
Blanen

Reputation: 782

How to make an alias to a specific node_module

We used to have a folder where which a bunch of typescript modules. Now we made that folder into a package.

The problem is that existing code uses webpack aliasses for that folder and now I want the same aliasses to point to the specific node_modules folder instead and this is not working.

alias: {
    x: path.resolve(__dirname, "node_modules/y")
    z: path.resolve(__dirname, "node_modules/y/z")
}

Should this work?

EDIT: It does work. It was just typescript still complaining and needed to add the aliases to tsconfig too.

Upvotes: 1

Views: 6284

Answers (2)

raythurnevoid
raythurnevoid

Reputation: 2824

In your case you don't need path.resolve(__dirname, ...) because webpack will look automatically in node_modules folder.

alias: {
    x: "y")
    z: "y/z")
}

With this code Webpack will look into your node_modules folder for "y" and "y/z" and aliases them to "x" and "z". then you can import like this: import myModule from "z".

If this is not working and you are using webpack from command line, probably the location of your webpack.config it's not correct, it should be in root at the same level of the node_modules folder.

Upvotes: 0

remix23
remix23

Reputation: 3044

This should work (alias inside resolve key of webpack config):

resolve: {
    alias: {
        x: path.resolve(__dirname, "node_modules/x")
    }
}

That said, as MonkeyTheDev said, aliasing x to x is pointless as it is the default behavior.

Upvotes: 1

Related Questions