简体   繁体   English

NPM - 添加到package.json但不安装

[英]NPM - Add to package.json but don't install

I am wondering if it is possible to run a command that would check that the package is a valid npm package, add it to package.json as a dependency, but not install it. 我想知道是否可以运行一个命令来检查包是否是一个有效的npm包,将它作为依赖项添加到package.json,但不安装它。

I am doing this because I have a certain package installed globally and need to require it for an open source project. 我这样做是因为我在全球范围内安装了某个软件包,并且需要将它用于开源项目。 Hence, I wish it to be included. 因此,我希望它被包括在内。

The correct way to only update package.json , without any other side-effects is: 只更新package.json而没有任何其他副作用的正确方法是:

npm install --save --package-lock-only --no-package-lock <package>

Use --package-lock-only to prevent writing to node_modules. 使用--package-lock-only防止写入node_modules。

The --package-lock-only argument will only update the package-lock.json, instead of checking node_modules and downloading dependencies. --package-lock-only参数只会更新package-lock.json,而不是检查node_modules和下载依赖项。

Then, use --no-package-lock to prevent the creation of the lock file: 然后,使用--no-package-lock来防止创建锁定文件:

The --no-package-lock argument will prevent npm from creating a package-lock.json file. --no-package-lock参数将阻止npm创建package-lock.json文件。 When running with package-lock's disabled npm will not automatically prune your node modules when installing. 在使用package-lock运行时,npm在安装时不会自动修剪节点模块。

See npm install docs for more. 有关更多信息,请参阅npm安装文档

I don't think yo can do that with npm. 我不认为你可以用npm做到这一点。 I've looked into the docs and I didn't find anything about. 我查看了文档,但没有找到任何相关信息。

You can use this as a workarround: 您可以将其用作工作场所:

npm i <package> --save && npm uninstall <package>

Hope it helps. 希望能帮助到你。

If your package is installed globally, I don't know if npm would reinstall it if you run: 如果您的软件包是全局安装的,我不知道如果运行,npm是否会重新安装它:

npm install --save foobar

That's what I would do to add it to package.json. 这就是我要把它添加到package.json的方法。

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

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