简体   繁体   English

npm链接,而不是安装,package.json依赖项

[英]npm link, not install, package.json dependencies

I want to local link all the explicit dependencies stated in my package.json . 我想本地链接在package.json声明的所有显式依赖项。

If I just try npm link what I get is a local install of all of the packages, independently of whether or not they are already globally installed. 如果我只是尝试使用npm link ,则得到的是所有软件包的本地安装,而与它们是否已全局安装无关。

I didn't expect that. 没想到 What I expected, and what I needed, is a behavior similar as if I'd do a npm link package . 我所期望的和所需要的是类似于我将要执行npm link package I wanted npm link to inspect the dependencies in package.json and for each of the, to create the link, and do a global install if needed. 我希望npm link检查package.json的依赖项,并为每一个创建链接,并在需要时进行全局安装。

npm link isn't designed to work that way. npm link并非旨在那样工作。 There are two ways to use it, and both depend on you downloading the dependency you want to link beforehand. 有两种使用方式,两种方式都取决于您事先下载要链接的依赖项。

First way (two steps) 第一种方式(两个步骤)

cd ../dependency npm link cd ../project npm link dependency

Second way (one step) 第二种方式(一步)

cd project npm link ../dependency

I think what you're trying to do is npm link where the target is a globally installed package (as opposed to a globally installed link to some directory on your filesystem). 我认为您要执行的操作是npm link ,其中目标是全局安装的软件包(而不是文件系统上某个目录的全局安装的链接)。 npm doesn't support that. npm不支持。

Ref: https://docs.npmjs.com/cli/link 参考: https : //docs.npmjs.com/cli/link

After some months of waiting, I've come out with my own implementation, that I am posting here for the record. 经过几个月的等待,我提出了自己的实现方式,我将其发布在这里进行记录。

I have two small scripts 我有两个小脚本

one packageDependencies.js that extracts all dependencies from a package.json . 一个packageDependencies.js ,它从package.json中提取所有依赖项。 Sschematically the code has: 从原理上讲,代码具有:

fs.readFile(process.argv[2]||'package.json','utf8',(err,data)=>{
  if(err) return console.error(err);
  var o = JSON.parse(data);
  for (var p in o.dependencies) console.log(p);
  for (var p in o.devDependencies) console.log(p);
});

and another npmlink.sh that iterates over that list and for each package, just npm --global install and npm link . 另一个npmlink.sh遍历该列表,对于每个软件包,只需npm --global installnpm link Schematically, 示意地,

for d in "$(node packageDependencies.js)"; do 
  npm --global install $d
  npm link $d
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM