npm安装如何工作 [英] How npm install works

查看:76
本文介绍了npm安装如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.json file that specifies which versions of which modules it needs.

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

Right now, I have a /node_modules directory 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_modules folder 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_modules directory with the correct app in it.

当我想回到旧项目并运行 npm install 以确保它仍然可以找到所需的所有依赖项。 (谁知道从那时起在父目录级别发生了什么搞笑业务。)我的印象是 npm install 这样做了:

Where this gets tricky is when I want to go back to an old project and run npm install to 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 install did this:


  • 对于 package.json 中列出的每个模块,首先检查它是否存在,向上移动目录同样的方式要求 。如果不是,请将其安装到本地 node_modules 目录(必要时创建该目录)。

  • for each module listed in package.json, first check if it's present, moving up the directory the same way require does. If it's not, install it to the local node_modules directory (creating that directory if necessary).

然而,当我在app文件夹中运行 npm install 时,它似乎在本地安装所有内容,无论它在上游可能存在的其他位置。这是正确的行为吗? (这可能是另一个原因,比如我的 package.json 中的坏版本语言)。如果这是正确的行为,有没有办法让我有 npm install 表现如上?

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?

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

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...

推荐答案


当我运行npm install inside但是,它似乎在本地安装所有内容,无论它在上游可能存在的其他位置。这是正确的行为吗? (这可能是另一个原因,比如我的package.json中的坏版本语言)。如果这是正确的行为,有没有办法让我的npm安装行为如上所述?

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?

是的,那是什么npm安装。在node.js代码中, require 算法有一个特定的地方序列,包括走向文件系统。但是, npm install 不会这样做。它只是安装到位。它使用的算法都只限于当前目录下的一个 node_modules 目录,并且它不会触及上面的任何内容(除了 - g )。

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


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

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...

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

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.

这篇关于npm安装如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆