简体   繁体   English

使用npm来安装或更新所需的软件包就像bundle for ruby​​gems一样

[英]Using npm to install or update required packages just like bundler for rubygems

I love Bundler , it's great at dependency management. 我喜欢Bundler ,它非常善于依赖管理。 I love npm , installing node packages is easy! 我爱npm ,安装节点包很容易! 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. 我有一个nodejs应用程序,并希望能够指定我的应用程序依赖项,并在我部署我的应用程序的任何地方轻松安装/更新它们 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. 我知道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实现类似的功能?

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文件中的步骤,默认情况下会得到),“bundle”不再是一个隔离的东西 - 它只是“它是如何工作的”。

So: 所以:

  1. Put a package.json file in the root of your project package.json文件放在项目的根目录中
  2. List your deps in that file 在该文件中列出您的deps

     { "name" : "my-project" , "version" : "1.0.0" , "dependencies" : { "express" : "1.0.0" } } 
  3. npm install Since you're calling this with no args, and not in global mode, it'll just install all your deps locally. npm install因为你在没有args的情况下调用它,而不是在全局模式下调用它,它只会在本地安装你所有的deps。

  4. require("express") and be happy. require("express")并且快乐。

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 你需要三个组件

  1. A subdirectory in your repository (ie deps/ ) 存储库中的子目录(即deps/
  2. A package.json file in the above directory that lists dependencies 上面目录中列出依赖项的package.json文件
  3. An index.js file in the above directory that requires your dependencies 上述目录中的index.js文件,需要您的依赖项

Example

Imagine that express is your only dependency 想象一下快递是你唯一的依赖

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. 现在,您应该能够使用npm安装依赖项。 You could even make this part of your deployment process 您甚至可以完成部署过程

cd deps
npm install

Then within your app code you can get access to your specific version of express like this: 然后在您的应用代码中,您可以访问您的特定版本的快递,如下所示:

var express = require('myapp_dependencies').express;

You should read these two articles from Isaacs(author npm) blog. 你应该阅读Isaacs(作者npm)博客中的这两篇文章。 I think they are really good, and I believe tell you how to achieve your goal: 我认为他们真的很好,我相信告诉你如何实现你的目标:

  1. http://blog.izs.me/post/1675072029/10-cool-things-you-probably-didnt-realize-npm-could-do http://blog.izs.me/post/1675072029/10-cool-things-you-probably-didnt-realize-npm-could-do
  2. http://foohack.com/2010/08/intro-to-npm/ http://foohack.com/2010/08/intro-to-npm/

I believe link #1(point #11) explains this: 我相信链接#1(第11点)解释了这个:

11: Bundle all your dependencies into the package itself 11:将所有依赖项捆绑到包本身中

When you use the npm bundle command, npm will put all your dependencies into the node_modules folder in your package. 当您使用npm bundle命令时,npm会将所有依赖项放入包中的node_modules文件夹中。 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/master This 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. npm bundle install http://github.com/whoever/whatever/tarball/master这会将该tarball的内容安装到bundle中,然后你可以将它列为依赖项,它不会尝试安装它您的包已安装。

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. 实际上,您可以在捆绑包上运行几乎任何npm命令。 To see what's inside, you can do npm bundle ls. 要查看内部的内容,您可以执行npm bundle ls。 To remove something, do npm bundle rm thing. 要删除某些东西,请执行npm bundle rm thing。 And, of course, you can install multiple versions and activate the one you want. 当然,您可以安装多个版本并激活您想要的版本。

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 . 从Npm版本1.1.2开始,有一个新命令npm shrinkwrap ,它创建一个类似于Gemfile.locknpm-shrinkwrapped.json Gemfile.lock文件。 It's important to make one, to prevent software rot (see Bundler's rationale ). 制作一个,以防止软件腐烂是很重要的(参见Bundler的基本原理 )。 Particularly as Nodejs has such a fast moving community. 特别是Nodejs拥有如此快速发展的社区。

While bundle install creates a Gemfile.lock automatically, npm install won't create npm-shrinkwrapped.json (but will use it when it exists). 虽然bundle install会自动创建一个Gemfile.lock ,但是npm install不会创建npm-shrinkwrapped.json (但会在它存在时使用它)。 Hence you need to remember to use npm shrinkwrap . 因此,您需要记住使用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/上的完整指南

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 . 在我看来,最简单的解决方案是使用一个package.json文件,其中private标志(仅在上个月添加到npm)设置为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. 这样,您可以运行npm installnpm 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; 运行npm install将在本地系统上安装express (如果它尚不存在); running npm publish gives an error because of the "private": true . 由于"private": true运行npm publish会出错。

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

Publish your app with npm as well, and list its dependencies in your package.json file. 使用npm发布您的应用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 软件包规范: http//wiki.commonjs.org/wiki/Packages/1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM