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

时间:2021-03-18 07:23:28

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.

我这样做是因为我在全局安装了某个软件包,并且需要为开源项目提供它。因此,我希望它被包括在内。

3 个解决方案

#1


2  

I don't think yo can do that with npm. I've looked into the docs and I didn't find anything about.

我不认为你可以用npm做到这一点。我查看了文档,但没有找到任何相关信息。

You can use this as a workarround:

您可以将其用作工作场所:

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

Hope it helps.

希望能帮助到你。

#2


1  

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. When running with package-lock's disabled npm will not automatically prune your node modules when installing.

--no-package-lock参数将阻止npm创建package-lock.json文件。在使用package-lock运行时,npm在安装时不会自动修剪节点模块。

See npm install docs for more.

有关更多信息,请参阅npm安装文档。

#3


-1  

If your package is installed globally, I don't know if npm would reinstall it if you run:

如果您的软件包是全局安装的,我不知道如果运行,npm是否会重新安装它:

npm install --save foobar

npm install --save foobar

That's what I would do to add it to the package.json

这就是我要把它添加到package.json中的方法

#1


2  

I don't think yo can do that with npm. I've looked into the docs and I didn't find anything about.

我不认为你可以用npm做到这一点。我查看了文档,但没有找到任何相关信息。

You can use this as a workarround:

您可以将其用作工作场所:

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

Hope it helps.

希望能帮助到你。

#2


1  

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. When running with package-lock's disabled npm will not automatically prune your node modules when installing.

--no-package-lock参数将阻止npm创建package-lock.json文件。在使用package-lock运行时,npm在安装时不会自动修剪节点模块。

See npm install docs for more.

有关更多信息,请参阅npm安装文档。

#3


-1  

If your package is installed globally, I don't know if npm would reinstall it if you run:

如果您的软件包是全局安装的,我不知道如果运行,npm是否会重新安装它:

npm install --save foobar

npm install --save foobar

That's what I would do to add it to the package.json

这就是我要把它添加到package.json中的方法