NPM link done at install time inside package.json

2019-09-11 02:42发布

问题:

I'm in the process of creating 2 projects. Project 1 will be like a library. Project 2 will be an application which uses Project 1's library code.

So I wish to do a npm link from Project 1 to Project 2. I can do this at the command line using the follow:

  • cd ../project1 npm link
  • cd ../project2 npm link project1_name

and it works fine. But I don't wish to do that, I want when I install the package.json to not only set up the various dependencies to also set up the link.

So how would I do that in a npm script? I thought possibly - "preinstall": "cd ../project1 npm link && cd ../project2 npm link project1_name",

but that fails and I think it may have to do with what's the correct way to split up the various commands.

回答1:

Right before I posted the question I tried a few more combinations and I got the answer -

You just need to place a && between each command that you'd enter if you were doing it manually.

So the answer is -

"preinstall": "cd ../project1 && npm link && cd ../project2 && npm link project1_name",

I placed that into the scripts object in the package.json and now whenever the package is installed it will link to the library project