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

后端 未结 6 917
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 03:57

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 de

6条回答
  •  北海茫月
    2021-01-30 04:33

    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;
    

提交回复
热议问题