Javascript npm 安装的工作原理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25897908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:02:47  来源:igfitidea点击:

How npm install works

javascriptnode.jsmodulenpm

提问by Chris Wilson

I use Node.js (via browserify) for each of my web apps, all of which have some dependencies in common and others specific to themselves. Each of these apps has a package.jsonfile that specifies which versions of which modules it needs.

我将 Node.js(通过browserify)用于我的每个 Web 应用程序,所有这些应用程序都有一些共同的依赖项和其他特定于自己的依赖项。这些应用程序中的每一个都有一个package.json文件,用于指定它需要哪些模块的哪些版本。

Right now, I have a /node_modulesdirectory in the parent folder of my apps for modules that they all need to reference, and then I put app-specific modules in a node_modulesfolder in that app's directory. This works fine in the short term, since my require()statements are able to keep looking upward in the file structure until they find the node_modulesdirectory with the correct app in it.

现在,我/node_modules在我的应用程序的父文件夹中有一个目录,用于存放它们都需要引用的模块,然后我将特定于应用程序的模块node_modules放在该应用程序目录中的一个文件夹中。这在短期内工作正常,因为我的require()语句能够在文件结构中继续向上查找,直到找到node_modules包含正确应用程序的目录。

Where this gets tricky is when I want to go back to an old project and run npm installto make sure it can still find all the dependencies it needs. (Who knows what funny-business has occurred since then at the parent directory level.) I was under the impression that npm installdid this:

当我想回到旧项目并运行npm install以确保它仍然可以找到它需要的所有依赖项时,这变得棘手。(谁知道从那以后在父目录级别发生了什么有趣的事情。)我的印象是npm install这样做:

  • for each module listed in package.json, first check if it's present, moving up the directory the same way requiredoes. If it's not, install it to the local node_modulesdirectory (creating that directory if necessary).
  • 在列出的每个模块package.json中,首先检查它是否存在,移动以同样的方式的目录require。如果不是,请将其安装到本地node_modules目录(如有必要,请创建该目录)。

When I run npm installinside an app folder, however, it appears to install everything locally regardless of where else it may exist upstream. Is that the correct behavior? (It's possible there's another reason, like bad version language in my package.json). If this IS the correct behavior, is there a way for me to have npm installbehave like the above?

npm install但是,当我在 app 文件夹中运行时,它似乎会在本地安装所有内容,而不管上游可能存在于何处。这是正确的行为吗?(可能还有另一个原因,比如我的package.json. 如果这是正确的行为,有没有办法让我npm install表现得像上面那样?

It's not a big deal to widely replicate the modules inside every app, but it feels messy and prevents me from make small improvements to the common modules and not having to update every old package.jsonfile. Of course, this could be a good thing...

在每个应用程序中广泛复制模块并不是什么大问题,但是感觉很混乱,并且阻止我对通用模块进行小的改进,而不必更新每个旧package.json文件。当然,这可能是一件好事......

采纳答案by Peter Lyons

When I run npm install inside an app folder, however, it appears to install everything locally regardless of where else it may exist upstream. Is that the correct behavior? (It's possible there's another reason, like bad version language in my package.json). If this IS the correct behavior, is there a way for me to have npm install behave like the above?

但是,当我在 app 文件夹中运行 npm install 时,它似乎会在本地安装所有内容,而不管上游可能存在于何处。这是正确的行为吗?(可能还有另一个原因,比如我的 package.json 中的版本语言不好)。如果这是正确的行为,有没有办法让 npm install 表现得像上面一样?

Yes, that is what npm install does. In node.js code, the requirealgorithm has a particular sequence of places it looks, including walking up the filesystem. However, npm installdoesn't do that. It just installs in place. The algorithms it uses are all constrained to just a single node_modulesdirectory under your current directory and it won't touch anything above that (except for with -g).

是的,这就是 npm install 所做的。在 node.js 代码中,require算法有一个特定的位置序列,包括遍历文件系统。但是,npm install不这样做。它只是安装到位。它使用的算法都仅限于node_modules当前目录下的单个目录,并且不会触及任何高于该目录的内容( with 除外-g)。

It's not a big deal to widely replicate the modules inside every app, but it feels messy and prevents me from make small improvements to the common modules and not having to update every old package.json file. Of course, this could be a good thing...

在每个应用程序中广泛复制模块并不是什么大不了的事,但感觉很混乱,并阻止我对通用模块进行小的改进,而不必更新每个旧的 package.json 文件。当然,这可能是一件好事......

Yeah basically you're doing it wrong. The regular workflow scales well to the Internet. For your use case it creates some extra tedious work, but you can also just use semantic versioning as intended and specify "mylib": "^1.0.0"in your package.json for your apps and be OK with automatically getting newer versions next time you npm install.

是的,基本上你做错了。常规工作流可以很好地扩展到 Internet。对于您的用例,它会创建一些额外繁琐的工作,但您也可以按预期使用语义版本控制并"mylib": "^1.0.0"在您的 package.json 中为您的应用程序指定,并在下次自动获取更新版本时确定npm install