Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package that is linked with npm link doesn't update

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/."

like image 547
SeBe Avatar asked Jun 13 '17 08:06

SeBe


People also ask

Does npm link override package json?

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.

How use npm linked 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} .


3 Answers

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:

  1. Rebuild linked project
  2. run npm link
  3. In host project run npm link dep
like image 104
Pavel Gurecki Avatar answered Sep 22 '22 21:09

Pavel Gurecki


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:

  • npm run clean deletes the dist folder. Useful if you have renamed files and the typecript compiler does not delete the old files but builds new ones in parallel
  • tsc to compile ts->js to the dist folder
  • npm rm my-lib -g && npm link to remove your lib and add it again from the global modules folder. I don't remember why I had to remove it first but it solved some problem I guess.
like image 45
andymel Avatar answered Sep 21 '22 21:09

andymel


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).

like image 38
FrostyDog Avatar answered Sep 21 '22 21:09

FrostyDog