Javascript 使用 npm 安装或更新所需的包,就像 rubygems 的 bundler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4871932/
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
Using npm to install or update required packages just like bundler for rubygems
提问by Daniel Beardsley
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 themwherever I deploy my app. This isn't a library I'm releasing, it's a full fledged web-app.
我喜欢Bundler,它在依赖管理方面非常出色。我喜欢npm,安装 node 包很容易!我有一个 nodejs 应用程序,希望能够指定我的应用程序依赖项,并在我部署应用程序的任何地方轻松安装/更新它们。这不是我要发布的库,而是一个成熟的网络应用程序。
I'm aware of the npm bundle
command, but that just seems to simply override the directory where packages are installed.
我知道该npm bundle
命令,但这似乎只是覆盖了安装软件包的目录。
I'm used to using bundler in this fashion:
我习惯于以这种方式使用 bundler:
# 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
仅当主机上不存在 rails v3.0.3 和任何其他必需的 gem 时才安装它
> bundle install
How can I achieve something similar with npm?
我怎样才能用 npm 实现类似的东西?
回答by isaacs
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".
从 npm 1.0 开始(如果您按照 README 文件中的步骤进行操作,现在默认情况下您会得到它),“捆绑”不再是一个隔离的东西——它只是“它是如何工作的”。
So:
所以:
- Put a
package.json
file in the root of your project List your deps in that file
{ "name" : "my-project" , "version" : "1.0.0" , "dependencies" : { "express" : "1.0.0" } }
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.
- 将
package.json
文件放在项目的根目录中 在该文件中列出您的 deps
{ "name" : "my-project" , "version" : "1.0.0" , "dependencies" : { "express" : "1.0.0" } }
npm install
由于您在没有参数的情况下调用它,而不是在全局模式下,它只会在本地安装所有的 deps。require("express")
而且要快乐。
回答by Daniel Beardsley
Edit: This only applies to npm versions < 1.0
编辑:这仅适用于 npm 版本 < 1.0
It was quite difficult to figure this out, but NPM makes this possible.
弄清楚这一点非常困难,但NPM 使这成为可能。
You need three components
你需要三个组件
- A subdirectory in your repository (i.e.
deps/
) - A
package.json
file in the above directory that lists dependencies - An
index.js
file in the above directory that requires your dependencies
- 存储库中的子目录(即
deps/
) - 一个
package.json
在上述目录中的文件,列出了相关 index.js
上面目录中的一个文件需要你的依赖
Example
例子
Imagine that expressis your only dependency
想象一下express是你唯一的依赖
deps/package.json
deps/package.json
note:Increment the version # each time you modify the dependencies
注意:每次修改依赖时增加版本#
{
"name": "myapp_dependencies",
"version": "0.0.1",
"engines": {
"node": "0.4.1"
},
"dependencies":{
"express": "2.0.0beta2"
}
}
deps/index.js
deps/index.js
export.modules = {
express: require('express')
//add more
}
Now you should be able to install your dependencies using npm. You could even make this part of your deployment process
现在您应该可以使用 npm 安装您的依赖项了。您甚至可以将此作为部署过程的一部分
cd deps
npm install
Then within your app code you can get access to your specific version of express like this:
然后在您的应用程序代码中,您可以像这样访问特定版本的 express:
var express = require('myapp_dependencies').express;
回答by Alfred
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:
您应该阅读 Isaacs(作者 npm)博客中的这两篇文章。我认为他们真的很好,我相信告诉你如何实现你的目标:
- http://blog.izs.me/post/1675072029/10-cool-things-you-probably-didnt-realize-npm-could-do
- http://foohack.com/2010/08/intro-to-npm/
- http://blog.izs.me/post/1675072029/10-cool-things-you-probably-didnt-realize-npm-could-do
- http://foohack.com/2010/08/intro-to-npm/
I believe link #1(point #11) explains this:
我相信链接 #1(point #11) 解释了这一点:
11: Bundle all your dependencies into the package itself
When you use the npm bundle command, npm will put all your dependencies into the node_modules folder in your package. But it doesn't stop there.
If you want to depend on something that's not on the registry, you can do that. Just do this:
npm bundle install http://github.com/whoever/whatever/tarball/masterThis will install the contents of that tarball into the bundle, and then you can list it as a dependency, and it won't try to install it when your package gets installed.
This also is handy if you have your own fork of something, and would prefer not to change the name.
In fact, you can run almost any npm command at the bundle. To see what's inside, you can do npm bundle ls. To remove something, do npm bundle rm thing. And, of course, you can install multiple versions and activate the one you want.
11:将所有依赖项捆绑到包本身中
当您使用 npm bundle 命令时,npm 会将您的所有依赖项放入您的包中的 node_modules 文件夹中。但它并不止于此。
如果您想依赖注册表中没有的内容,您可以这样做。只需这样做:
npm bundle install http://github.com/whoever/whatever/tarball/master这会将那个tarball的内容安装到bundle中,然后你可以将它列为依赖项,它不会尝试安装它您的软件包已安装。
如果您有自己的叉子,并且不想更改名称,这也很方便。
实际上,您几乎可以在包中运行任何 npm 命令。要查看里面的内容,您可以执行 npm bundle ls。要删除某些内容,请执行 npm bundle rm 操作。当然,您可以安装多个版本并激活您想要的版本。
回答by Colonel Panic
As of Npm version 1.1.2 , there's a new command npm shrinkwrap
which creates an npm-shrinkwrapped.json
file, analogous to Gemfile.lock
. It's important to make one, to prevent software rot (see Bundler's rationale). Particularly as Nodejs has such a fast moving community.
从 Npm 版本 1.1.2 开始,有一个新命令npm shrinkwrap
可以创建一个npm-shrinkwrapped.json
文件,类似于Gemfile.lock
. 制作一个很重要,以防止软件腐烂(请参阅Bundler 的基本原理)。特别是因为 Nodejs 拥有如此快速发展的社区。
While bundle install
creates a Gemfile.lock
automatically, npm install
won't create npm-shrinkwrapped.json
(but will use it when it exists). Hence you need to remember to use npm shrinkwrap
.
虽然bundle install
会Gemfile.lock
自动npm install
创建npm-shrinkwrapped.json
,但不会创建(但会在它存在时使用它)。因此,您需要记住使用npm shrinkwrap
.
Read a full guide at http://blog.nodejs.org/2012/02/27/managing-node-js-dependencies-with-shrinkwrap/
在http://blog.nodejs.org/2012/02/27/managing-node-js-dependencies-with-shrinkwrap/阅读完整指南
回答by Trevor Burnham
It seems to me that the simplest solution is to use a package.json
file with the private
flag (added to npm just last month) set to true
. That way, you can run npm install
or npm bundle
to grab your project's dependencies, but you prevent anyone from accidentally publishing your non-public project.
在我看来,最简单的解决方法是使用一个package.json
文件与private
设置标志(添加到故宫就在上个月)true
。这样,您可以运行npm install
或npm bundle
获取项目的依赖项,但可以防止任何人意外发布您的非公开项目。
Here's an example package.json
:
这是一个例子package.json
:
{
"name": "yourProject"
,"version": "1.0.0"
,"dependencies": { "express" : ">=2.1.0" }
,"private": true
}
Running npm install
will install express
on the local system if it doesn't already exist; running npm publish
gives an error because of the "private": true
.
如果本地系统不存在,则运行npm install
将安装express
在本地系统上;跑步npm publish
给了因为一个错误"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 ls installed
.
回答by Dan Grossman
Publish your app with npm
as well, and list its dependencies in your package.json file.
发布您的应用程序npm
,并在您的 package.json 文件中列出其依赖项。
When someone uses npm
to install your package, npm
will take care of resolving its dependencies.
当有人用来npm
安装你的包时,npm
会负责解决它的依赖关系。
Packages spec: http://wiki.commonjs.org/wiki/Packages/1.0