简体   繁体   English

npm如何安装只保存对package.json的依赖?

[英]How to npm install to only save dependency to package.json?

I'm adding dependencies to a package.json that will be used as part of a provisioning process for a virtual machine.我正在将依赖项添加到package.json ,它将用作虚拟机配置过程的一部分。 As such, I don't actually need to install the modules locally since the provisioner will do that for me inside the VM.因此,我实际上不需要在本地安装模块,因为配置器会在 VM 内为我安装这些模块。 So is there any way to do the following:那么有什么办法可以做到以下几点:

npm install --save <module>

So that it only creates a dependency for the latest version of the module in package.json without actually downloading the module or creating a node_modules folder?这样它只package.json中的最新版本的模块创建依赖关系,而不实际下载模块或创建node_modules文件夹?

The --dry-run option is close, as it doesn't create a node_modules folder but it also doesn't write to package.json either. --dry-run选项关闭,因为它不会创建node_modules文件夹,但它也不会写入package.json

For now, I'm manually doing the following each time I need to update packages before re-provisioning the VM:现在,每次我需要在重新配置 VM 之前更新软件包时,我都会手动执行以下操作:

rm -rf node_modules

Other reasons for this might include being able to easily build a package.json file in low-bandwidth situations such as tethering, where you know you'll need the module eventually but don't want to spare the bandwidth.其他原因可能包括能够在低带宽情况下轻松构建package.json文件,例如网络共享,您知道最终需要该模块但不想节省带宽。

Was searching for the solution.正在寻找解决方案。 Haven't found, then made a script which adds dependencies (latest or specified versions) to the package.json file skipping the installation process.没有找到,然后制作了一个脚本,将依赖项(最新或指定版本)添加到package.json文件中,跳过安装过程。

https://www.npmjs.com/package/add-dependencies https://www.npmjs.com/package/add-dependencies

Installation安装

If not using with npx (see below):如果不与npx一起npx (见下文):

$ npm install add-dependencies [-g]

Usage用法

Run:运行:

$ add-dependencies [package_file] <dependencies> [target] [--no-overwrite]

or with npx :或使用npx

$ npx add-dependencies [package_file] <dependencies> [target] [--no-overwrite]

where dependencies is the list of dependencies divided by space, and target is one of the following:其中dependencies是按空间划分的依赖项列表, target是以下之一:

  • --dev / --save-dev / -D for devDependencies --dev / --save-dev / -D用于devDependencies
  • --peer / --save-peer / -P for peerDependencies --peer / --save-peer / -P用于peerDependencies
  • --optional / --save-optional / -O for optionalDependencies --optional / --save-optional / -O用于optionalDependencies

If no target argument passed, dependencies are written to dependencies .如果没有传递target参数,则将依赖项写入dependencies

If no package_file argument passed, the script searches for a package.json file within the current working directory.如果没有传递package_file参数,脚本会在当前工作目录中搜索package.json文件。

Use --no-overwrite flag to prevent already existing packages in package.json from being overwritten.使用--no-overwrite标志来防止package.json已经存在的包被覆盖。

Example:示例:

$ add-dependencies /home/user/project/package.json moment@2.0.0 react@16.8 redux eslint --dev

or with npx :或使用npx

$ npx add-dependencies /home/user/project/package.json moment@2.0.0 react@16.8 redux eslint --dev

Hope this could help someone else.希望这可以帮助别人。

There is no way to do that with npm that I'm aware of.我所知道的npm无法做到这一点。

There are two npm packages for doing this;有两个 npm 包可以做到这一点; I've never used either of them, but they might be worth a try:我从未使用过它们中的任何一个,但它们可能值得一试:

Hope this helps!希望这有帮助!

Interestingly combining --package-lock-only with --no-package-lock seems to do this有趣的是将--package-lock-only--no-package-lock结合起来似乎可以做到这一点

npm install --package-lock-only --no-package-lock PACKAGE

This does not create or update the package-lock.json file.这不会创建或更新 package-lock.json 文件。 Only adds an entry to the package.json只向 package.json 添加一个条目

UPDATE更新

This was actually a bug and is now fixed in npm 6.9.0这实际上是一个错误,现在已在 npm 6.9.0 中修复

https://github.com/npm/cli/pull/146 https://github.com/npm/cli/pull/146

https://npm.community/t/release-npm-6-9-0/5911 https://npm.community/t/release-npm-6-9-0/5911

As of Npm > 8 the correct answer is从 Npm > 8 起,正确答案是

npm pkg set devDependencies.rollup="*" devDependencies.typescript="4.5"

Explaination npm pkg set can change your current package.json npm pkg set可以改变你当前的package.json

see: https://docs.npmjs.com/cli/v7/commands/npm-pkg见: https://docs.npmjs.com/cli/v7/commands/npm-pkg

I made a very simple way to do this.我做了一个非常简单的方法来做到这一点。

Add this to the scripts section of your package.json :将此添加到package.jsonscripts部分:

  "scripts": {
    "no-dl": "npm install --package-lock-only --no-package-lock"
  }

Then use it like this:然后像这样使用它:

npm run no-dl <package-name>

I've called it no-dl — pick your own name as needed.我称它为no-dl — 根据需要选择您自己的名字。

npm install --save packagename然后npm uninstall packagename (没有 --save 标志)完成了这个,虽然创建了一个空的 node_modules 文件夹

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

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