Using npm to install or upgrade required packages such as bundler for rubygems

I like the Bundler , this is great for dependency management. I like npm , installing node packages is easy! I have a nodejs application, and I would like it to be able to specify application dependencies and easily install / update them wherever I deploy my application. This is not a library that I release, it is a full-fledged web application.

I know the npm bundle command, but it just simply overrides the directory where the packages are installed.

I use bundler this way:

 # Gemfile gem "rails", "3.0.3" 

Install rails v3.0.3 and any other required gems on the host machine only if it does not already exist

 > bundle install 

How can I achieve something like this with npm?

+86
javascript npm dependencies bundler
Feb 02 2018-11-11T00:
source share
6 answers

Starting with npm 1.0 (now this is what you get by default if you follow the steps in the README file), the "bundle" is no longer split - it is just "how it works."

So:

  • Put the package.json file in the root of your project
  • List of your fingerprints in this file.

     { "name" : "my-project" , "version" : "1.0.0" , "dependencies" : { "express" : "1.0.0" } } 
  • npm install Since you are calling this with no arguments, and not in global mode, it just installs all your depicks locally.

  • require("express") and be happy.
+140
May 16 '11 at 20:42
source share

Edit: this only applies to npm versions <1.0




It was hard to figure it out, but NPM makes it possible .

You need three components

  • Subdirectory in your repository (i.e. deps/ )
  • The package.json file in the above directory listing the dependencies
  • The index.js file in the above directory, which requires your dependencies

Example

Imagine express is your only addiction

Deps / package.json

Note. Increase version # each time the dependency changes

 { "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 can install your dependencies using npm. You can even do this part of the deployment process.

 cd deps npm install 

Then, in the application code, you can access your specific version of the expression as follows:

 var express = require('myapp_dependencies').express; 
+10
Mar 13 2018-11-11T00:
source share

You should read these two articles from the Isaacs blog (author npm). I think they are really good, and I believe that I will tell you how to achieve my goal:

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

11: Link all your dependencies in the package itself

When you use the npm bundle command, npm will put all your dependencies in node_modules in your package. But he does not stop there.

If you want to depend on something that is not in the registry, you can do that. Just do the following:

Installing the npm package http://github.com/whoever/whatever/tarball/master This will install the contents of this tarball into the kit, and then you can list it as a dependency, and do not try to install it when your package is installed.

It is also convenient if you have something, and prefer not to change the name.

In fact, you can run almost any npm command in the kit. To find out what's inside, you can do npm bundle ls. to delete anything, do the npm bundle rm thing. And, of course, you can install several versions and activate what you want.

+5
Feb 02 '11 at 12:33
source share

It seems to me that the easiest solution is to use the package.json file with the private flag (added to the number per minute only last month) set to true . That way, you can run npm install or npm bundle to capture project dependencies, but you did not allow anyone to accidentally publish your non-public project.

Here is 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 does not already exist; running npm publish gives an error due to "private": true .

You and your team can use the version tag internally to track dependency changes over time - every time you change a dependency, hit the version. To find out which version has been installed, use npm ls installed .

+2
Mar 30 2018-11-11T00:
source share

In Npm version 1.1.2, a new npm shrinkwrap has been created that creates an npm-shrinkwrapped.json file similar to Gemfile.lock . It is important to do this to prevent software crashes (see Bundler's explanation ). In particular, Nodejs has such a fast-paced community.

While bundle install automatically creates Gemfile.lock , npm install does not create npm-shrinkwrapped.json (but will use it when it exists). Therefore you need to remember npm shrinkwrap .

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

+2
Feb 20 '13 at 14:56
source share

Publish the application using npm , and also specify its dependencies in the package.json file.

When someone uses npm to install your package, npm will take care of resolving its dependencies.

Package Specification: http://wiki.commonjs.org/wiki/Packages/1.0

+1
Feb 02 '11 at 7:52
source share



All Articles