Link NPM Package And Use It

yahone chow
1 min readOct 29, 2021

Let’s say you have two project A and B, B depends on A, which you are working on B but also A sometimes.

You’ve npm installed A in B, once A’s file changed you need npm install A in B again in every time.

It’s a piece of shit, right?

NPM provided resolve solution for that actually.

Enter the project’s root directory that other project depends on

# this will create global symlink,
# it links to where you've run below code
# the global symlink name same as name property in the package.json
npm link

Then, In anywhere you want

npm link SYMLINK_NAME

That’s all

Try to modify any file in project A, and check it in B’s node_modules/SYMLINK_NAME

Perhaps you are using webpack-dev-server too, you noticed file changed but never trigger webpack file watch and HMR, put this in your devServer.

watchOptions: {
followSymlinks: true,
}

https://webpack.js.org/configuration/watch/#watchoptionsfollowsymlinks

https://docs.npmjs.com/cli/v7/commands/npm-link

--

--