复制node_modules是否安全?

时间:2021-01-26 10:48:47

If I want to copy a node project: Does it make any difference if I just copy node_modules or install all the modules again from scratch via npm?

如果我想复制节点项目:如果我只是复制node_modules或通过npm从头开始再次安装所有模块,它会有什么不同吗?

1 个解决方案

#1


11  

2017-05-12

I've updated this answer to reflect changes since the release of npm 3.x and new tools that are available.

我已经更新了这个答案,以反映自npm 3.x发布以来可用的新工具的变化。

npm v3 dependency installation is now non-deterministic meaning you may get different packages depending on the order in which packages have been installed over time. This isn't necessarily a bad thing, just something to be aware of.

npm v3依赖项安装现在是非确定性的,这意味着您可能会获得不同的软件包,具体取决于软件包随时间安装的顺序。这不一定是坏事,只是需要注意的事情。

Given this change I personally don't copy my node_modules directory around too much (it's still possible though!) and instead opt for a clean install most of the time.

鉴于这种变化,我个人不会过多地复制我的node_modules目录(虽然它仍然可以!)而是大多数时候选择干净安装。

There are new tools like Yarn Package Manager which can speed up the installation process if you are doing that a lot (but as of 2017-05-12 it's unclear how well it handles private npm organisations and private scoped packages).

有一些新的工具,比如Yarn Package Manager,如果你做了很多事情,可以加快安装过程(但是从2017-05-12开始,还不清楚它处理私有npm组织和私有范围包的程度如何)。

So the takeaway is still pretty much the same: it won't hurt, but maybe err on the side of a clean install. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.

所以外卖仍然几乎相同:它不会受到伤害,但可能在干净安装方面犯错。如果确实发生了奇怪的事情而你遇到了问题,那么你可以删除node_modules并运行npm install。


Original answer from 2014-06-08:

In general it should be fine - I copy the node_modules directory sometimes from my other projects to speed up the setup process.

一般情况下应该没问题 - 我有时会从其他项目中复制node_modules目录以加快设置过程。

You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required.

您始终可以复制node_modules,然后在新项目中运行npm install或npm update,以确保您拥有最新版本。 npm将使用node_modules中的文件作为缓存,并且只应在需要时关闭较新的内容。

In short: it won't hurt. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.

简而言之:它不会受到伤害。如果确实发生了奇怪的事情而你遇到了问题,那么你可以删除node_modules并运行npm install。

#1


11  

2017-05-12

I've updated this answer to reflect changes since the release of npm 3.x and new tools that are available.

我已经更新了这个答案,以反映自npm 3.x发布以来可用的新工具的变化。

npm v3 dependency installation is now non-deterministic meaning you may get different packages depending on the order in which packages have been installed over time. This isn't necessarily a bad thing, just something to be aware of.

npm v3依赖项安装现在是非确定性的,这意味着您可能会获得不同的软件包,具体取决于软件包随时间安装的顺序。这不一定是坏事,只是需要注意的事情。

Given this change I personally don't copy my node_modules directory around too much (it's still possible though!) and instead opt for a clean install most of the time.

鉴于这种变化,我个人不会过多地复制我的node_modules目录(虽然它仍然可以!)而是大多数时候选择干净安装。

There are new tools like Yarn Package Manager which can speed up the installation process if you are doing that a lot (but as of 2017-05-12 it's unclear how well it handles private npm organisations and private scoped packages).

有一些新的工具,比如Yarn Package Manager,如果你做了很多事情,可以加快安装过程(但是从2017-05-12开始,还不清楚它处理私有npm组织和私有范围包的程度如何)。

So the takeaway is still pretty much the same: it won't hurt, but maybe err on the side of a clean install. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.

所以外卖仍然几乎相同:它不会受到伤害,但可能在干净安装方面犯错。如果确实发生了奇怪的事情而你遇到了问题,那么你可以删除node_modules并运行npm install。


Original answer from 2014-06-08:

In general it should be fine - I copy the node_modules directory sometimes from my other projects to speed up the setup process.

一般情况下应该没问题 - 我有时会从其他项目中复制node_modules目录以加快设置过程。

You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required.

您始终可以复制node_modules,然后在新项目中运行npm install或npm update,以确保您拥有最新版本。 npm将使用node_modules中的文件作为缓存,并且只应在需要时关闭较新的内容。

In short: it won't hurt. If something weird does happen and you run into problems then you can just delete node_modules and run npm install.

简而言之:它不会受到伤害。如果确实发生了奇怪的事情而你遇到了问题,那么你可以删除node_modules并运行npm install。