使用 npm 安装或更新所需的软件包,就像 rubygems 的捆绑器一样
我喜欢 Bundler,它非常适合依赖管理。我喜欢 npm,安装节点包很简单!我有一个 Nodejs 应用程序,并且希望能够能够指定我的应用程序依赖项并轻松安装/更新它们无论我在何处部署应用程序。这不是我发布的库,而是一个成熟的网络应用程序。
我知道 npm bundle 命令,但这似乎只是覆盖了安装包的目录。
我习惯以这种方式使用捆绑器:
# Gemfile
gem "rails", "3.0.3"
仅当主机上尚不存在时才安装 Rails v3.0.3 和任何其他必需的 gem
> bundle install
如何使用 npm 实现类似的功能?
I love Bundler, it's great at dependency management. I love npm, installing node packages is easy! I have a nodejs app and would love to be able to specify my apps dependencies and easily install / update them wherever I deploy my app. This isn't a library I'm releasing, it's a full fledged web-app.
I'm aware of the npm bundle
command, but that just seems to simply override the directory where packages are installed.
I'm used to using bundler in this fashion:
# Gemfile
gem "rails", "3.0.3"
Installs rails v3.0.3 and any other required gems on the host machine only if it doesn't already exist
> bundle install
How can I achieve something similar with npm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 npm 1.0 开始(如果您按照 README 文件中的步骤操作,现在默认情况下会得到这个),“捆绑包”不再是一个隔离的东西 - 它只是“它是如何工作的”。
因此:
package.json
文件放在项目的根目录中在该文件中列出您的 deps
npm install
因为您在没有参数的情况下调用此函数,并且不是在全局模式下,所以它只会安装您的所有 deps本地。require("express")
并感到高兴。As of npm 1.0 (which is now what you get by default if you follow the steps in the README file), "bundle" is no longer a segregated thing -- it's just "how it works".
So:
package.json
file in the root of your projectList your deps in that file
npm install
Since you're calling this with no args, and not in global mode, it'll just install all your deps locally.require("express")
and be happy.编辑:这仅适用于 npm 版本 < 1.0
弄清楚这一点相当困难,但NPM 使这成为可能。
您需要三个组件
示例
想象一下 express 是您唯一的依赖项
deps/package.json
注意: 每次修改依赖项时增加版本 #
deps/index.js
现在您应该能够使用 npm 安装依赖项。您甚至可以将这部分作为部署过程的一部分
然后在您的应用程序代码中您可以访问特定版本的express,如下所示:
Edit: This only applies to npm versions < 1.0
It was quite difficult to figure this out, but NPM makes this possible.
You need three components
deps/
)package.json
file in the above directory that lists dependenciesindex.js
file in the above directory that requires your dependenciesExample
Imagine that express is your only dependency
deps/package.json
note: Increment the version # each time you modify the dependencies
deps/index.js
Now you should be able to install your dependencies using npm. You could even make this part of your deployment process
Then within your app code you can get access to your specific version of express like this:
您应该阅读 Isaacs(作者 npm)博客中的这两篇文章。我认为它们真的很好,并且我相信会告诉您如何实现您的目标:
我相信链接#1(点#11)解释了这一点:
You should read these two articles from Isaacs(author npm) blog. I think they are really good, and I believe tell you how to achieve your goal:
I believe link #1(point #11) explains this:
从 Npm 版本 1.1.2 开始,有一个新命令
npm Shrinkwrap
它创建一个npm-shrinkwrapped.json
文件,类似于Gemfile.lock
。制作一个非常重要,以防止软件崩溃(请参阅Bundler 的基本原理)。特别是 Nodejs 拥有如此快速发展的社区。虽然
bundle install
自动创建Gemfile.lock
,但npm install
不会创建npm-shrinkwrapped.json
(但当它存在时会使用它)。因此,您需要记住使用npm Shrinkwrap
。请访问 http://blog 阅读完整指南.nodejs.org/2012/02/27/managing-node-js-dependencies-with-shrinkwrap/
As of Npm version 1.1.2 , there's a new command
npm shrinkwrap
which creates annpm-shrinkwrapped.json
file, analogous toGemfile.lock
. It's important to make one, to prevent software rot (see Bundler's rationale). Particularly as Nodejs has such a fast moving community.While
bundle install
creates aGemfile.lock
automatically,npm install
won't createnpm-shrinkwrapped.json
(but will use it when it exists). Hence you need to remember to usenpm shrinkwrap
.Read a full guide at http://blog.nodejs.org/2012/02/27/managing-node-js-dependencies-with-shrinkwrap/
在我看来,最简单的解决方案是使用
package.json
文件,并将private
标志(上个月添加到 npm)设置为true.这样,您可以运行
npm install
或npm bundle
来获取项目的依赖项,但可以防止任何人意外发布您的非公开项目。下面是一个
package.json
示例:运行
npm install
将在本地系统上安装express
(如果尚不存在);由于"private": true
,运行npmpublish
会出现错误。您和您的团队可以在内部使用版本标记来跟踪依赖项随时间的变化 - 每次更改依赖项时,都会更改版本。要查看您安装的版本,请使用
npm lsinstalled
。It seems to me that the simplest solution is to use a
package.json
file with theprivate
flag (added to npm just last month) set totrue
. That way, you can runnpm install
ornpm bundle
to grab your project's dependencies, but you prevent anyone from accidentally publishing your non-public project.Here's an example
package.json
:Running
npm install
will installexpress
on the local system if it doesn't already exist; runningnpm publish
gives an error because of the"private": true
.You and your team can use the version tag internally to track dependency changes over time—each time you change a dependency, bump the version. To see which version you've installed, use
npm ls installed
.也使用
npm
发布您的应用,并在 package.json 文件中列出其依赖项。当有人使用
npm
安装您的软件包时,npm
将负责解决其依赖关系。包规范:http://wiki.commonjs.org/wiki/Packages/1.0
Publish your app with
npm
as well, and list its dependencies in your package.json file.When someone uses
npm
to install your package,npm
will take care of resolving its dependencies.Packages spec: http://wiki.commonjs.org/wiki/Packages/1.0