使用 npm 安装或更新所需的软件包,就像 ruby​​gems 的捆绑器一样

发布于 2024-10-15 07:07:24 字数 521 浏览 6 评论 0原文

我喜欢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

栖迟 2024-10-22 07:07:24

从 npm 1.0 开始(如果您按照 README 文件中的步骤操作,现在默认情况下会得到这个),“捆绑包”不再是一个隔离的东西 - 它只是“它是如何工作的”。

因此:

  1. package.json 文件放在项目的根目录中
  2. 在该文件中列出您的 deps

    { "名称" : "我的项目"
    ,“版本”:“1.0.0”
    , "依赖项" : { "express" : "1.0.0" } }
    
  3. npm install 因为您在没有参数的情况下调用此函数,并且不是在全局模式下,所以它只会安装您的所有 deps本地。

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

  1. Put a package.json file in the root of your project
  2. List your deps in that file

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

  4. require("express") and be happy.
坏尐絯 2024-10-22 07:07:24

编辑:这仅适用于 npm 版本 < 1.0


弄清楚这一点相当困难,但NPM 使这成为可能

您需要三个组件

  1. 存储库中的子目录(即 deps/)
  2. 上述目录中列出依赖项的 package.json 文件
  3. index.js 上述目录中需要依赖项的文件

示例

想象一下 express 是您唯一的依赖项

deps/package.json

注意: 每次修改依赖项时增加版本 #

{
  "name": "myapp_dependencies",
  "version": "0.0.1",
  "engines": {
    "node": "0.4.1"
  },
  "dependencies":{
    "express": "2.0.0beta2"
  }
}

deps/index.js

export.modules = {
  express: require('express')
  //add more
}

现在您应该能够使用 npm 安装依赖项。您甚至可以将这部分作为部署过程的一部分

cd deps
npm install

然后在您的应用程序代码中您可以访问特定版本的express,如下所示:

var express = require('myapp_dependencies').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

  1. A subdirectory in your repository (i.e. deps/)
  2. A package.json file in the above directory that lists dependencies
  3. An index.js file in the above directory that requires your dependencies

Example

Imagine that express is your only dependency

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

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

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;
鯉魚旗 2024-10-22 07:07:24

您应该阅读 Isaacs(作者 npm)博客中的这两篇文章。我认为它们真的很好,并且我相信会告诉您如何实现您的目标:

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

我相信链接#1(点#11)解释了这一点:

11:将所有依赖项捆绑到包本身

当您使用
npm 捆绑命令,npm 将把所有
你的依赖关系到
包中的 node_modules 文件夹。
但它并不止于此。

如果你想依赖某事
这不在注册表中,你可以这样做
那。只需这样做:

npm 捆绑安装
http://github.com/whoever/whatever/tarball/master
这将安装该内容
tarball 到包中,然后你
可以将其列为依赖项,并且它
当您的
软件包已安装。

如果您有的话,这也很方便
自己的叉子,并且会
不想更改名称。

事实上,您几乎可以运行任何 npm
捆绑包中的命令。看看是什么
在里面,你可以执行 npm bundle ls。到
删除一些东西,执行 npm bundle rm
事物。当然,您可以安装
多个版本并激活一个
你想要的。

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:

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

I believe link #1(point #11) explains this:

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

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.

画尸师 2024-10-22 07:07:24

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

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.

Read a full guide at http://blog.nodejs.org/2012/02/27/managing-node-js-dependencies-with-shrinkwrap/

祁梦 2024-10-22 07:07:24

在我看来,最简单的解决方案是使用 package.json 文件,并将 private 标志(上个月添加到 npm)设置为 true.这样,您可以运行 npm installnpm bundle 来获取项目的依赖项,但可以防止任何人意外发布您的非公开项目。

下面是一个 package.json 示例:

{
"name": "yourProject"
,"version": "1.0.0"
,"dependencies": { "express" : ">=2.1.0" }
,"private": true
}

运行 npm install 将在本地系统上安装 express(如果尚不存在);由于 "private": true,运行 npmpublish 会出现错误。

您和您的团队可以在内部使用版本标记来跟踪依赖项随时间的变化 - 每次更改依赖项时,都会更改版本。要查看您安装的版本,请使用 npm lsinstalled

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.

Here's an example 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.

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.

好多鱼好多余 2024-10-22 07:07:24

也使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文