NPM链接在安装时在package.json中完成

NPM链接在安装时在package.json中完成

问题描述:

我正在创建2个项目. 项目1就像一个图书馆. 项目2将是使用项目1的库代码的应用程序.

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.

所以我希望建立一个从Project 1到Project 2的npm链接.我可以在命令行中使用以下命令执行此操作:

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链接
  • cd ../project2 npm链接project1_name

,它工作正常.但是我不想这样做,我想在安装package.json时不仅要设置各种依赖关系,还要设置链接.

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.

那么我将如何在npm脚本中做到这一点? 我认为可能-预安装":"cd ../project1 npm链接&& cd ../project2 npm链接project1_name",

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.

就在我发布问题之前,我尝试了更多组合,然后得到了答案-

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.

所以答案是-

预安装":"cd ../project1&&& cd cd ../project2&& npm链接project1_name",

我将其放入package.json中的scripts对象中,现在无论何时安装软件包,它都将链接到库项目

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