I have two modules - my main project and a component library - where I want to link the lib to the main project. Both is working with webpack and react. So I did:
In comp-lib dir:
npm link
In project dir:
npm link comp-lib
The package is showing up in the node_modules folder, but when I work on the component library the changes are not reflected in main project. What am I missing out?
I couldn't really find something about this issue on Google and the npm link doc just says about the example: "Now, any changes to ~/projects/node-redis will be reflected in ~/projects/node-bloggy/node_modules/node-redis/."
According the npm docs, npm link is not intended to change your package. json . It creates symbolic links on your file system for a package.
Package linking is a two-step process. First, npm link in a package folder with no arguments will create a symlink in the global folder {prefix}/lib/node_modules/<package> that links to the package where the npm link command was executed. It will also link any bins in the package to {prefix}/bin/{name} .
Had similar problem with linked package - npm link dep
was picking up old version of "dep".
Running npm link
(again) in "dep" folder solved the problem. Theoretically it should be called only once, but for some reason it gets out of sync sometimes.
Thus in order to reliably update linked project, execute these steps after each change:
npm link
npm link dep
I don't remember exactly what problems I had and I am also not sure if all of that is necessary but for me it works great.
I added the following mini script to the package.json scripts list
"scripts": { "clean": "if exist dist ( rd /S /Q dist)", "updateLink": "npm run clean && tsc && npm rm my-lib -g && npm link" }
(Replace "my-lib" with your package name)
Then simply call npm run updateLink
whenever you change something in the lib.
What it does:
Make sure that the Node versions of the main project and dependency project match precisely.
If you use nvm
to manage multiple projects on multiple node versions, the npm link
will produce the symbolic link only in the node version that npm link
was initiated from (i.e., the dependency project).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With