简体   繁体   中英

How do I install specific versions / tags of all npm dependencies

We have a large project involving multiple node modules. We have 3 levels in our development model.

  • develop
  • stage
  • production

We would like to publish modules to our private registry with develop, stage and production tags and expect CI tool to build and install the deps based on these tags.

Now for deploying to development environment CI can just do npm install on the parent module and all deps with latest versions gets installed. No worries here.

However if CI wants to push something to stage / production environment how do I tell npm install to pick dependency versions tagged as stage / production .

I know if I want to install a particular version of a dependency I can specify npm install package@version . But since we have very huge number of dependencies this approach is not feasible for us.

We are open to suggestions for altering our development model as well. Any help would be appreciated.

One way to do this would be to store the versions in a separate package.json for each environment.

For instance you could have a /packages directory with 3 files dev.package.json, stage.package.json, prod.package.json.

Have your CI process copy the correct package.json for the environment it is installing in.

I'm not sure what the CI process looks like, so I'll give an example that uses a shell script, deploy.sh for linux.

#!/bin/bash

ENV='production'
if [[ `hostname` =~ \.development\. ]]; then
  ENV='development'
fi
if [[ `hostname` =~ \.stage\. ]]; then
  ENV='stage'
fi

echo "$ENV environment detected"

cp packages/$ENV.package.json package.json

npm install

This assumes the server's hostnames can help indicate the environment and the CI can run shell scripts. You may need something different depending on your setup.

For instance, you probably will already know the env used within your CI process without the hostname check part but I added it just in case.

With this type of solution you can have each env package.json specify the exact module versions required for the environment and use npm install to get them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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