简体   繁体   中英

Run npm install --production on OpenShift

When I push my code to OpenShift, it looks like it's installing my devDependencies which takes forever . I would really love to set it up so it will only install the dependencies (by running with the --production flag). Is there any way to do this?

You can tell npm to install using the --production flag by setting the NPM_CONFIG_PRODUCTION environment variable to " true ".

Here is an example that should work for existing applications:

rhc env set NPM_CONFIG_PRODUCTION="true"

Or, you can set this variable as a part of your initial app-create step:

rhc app create myapplication nodejs-0.10 NPM_CONFIG_PRODUCTION="true"

Found a way to specify it in source instead of during app creation. The benefit (for me) over an env var is that it applies to all ways to launch the app, including a "Launch on OpenShift" button.

Create an .openshift/action_hooks/pre_build file:

#!/bin/bash
# This makes npm not install devDependencies.
echo 'Enabling npm production'
echo 'production = true' >> $OPENSHIFT_REPO_DIR/.npmrc

That's it! I've tested and it does affect npm for this build, and the .npmrc disappears if you remove this hook in the future.

(Obviously I could also achieve this by simply adding an .npmrc to my repo, but do not want to affect people checking out the source and running npm install , only how it works on OpenShift.)

It looks like the only solution is to update the cartridge itself. The npm install command is located in the cartridge's bin/control folder. Meanwhile, it's been fixed in the originating github repo at wshearn/openshift-origin-cartridge-nodejs so you can just install from github rather than using the Quickstart.

  1. Create a .npmrc file where the node_modules folder is located.

  2. Open it with your text-editor and add this to it:

    production = true

PS no semicolons or any other characters

This will ensure that devDependencies are not installed on the OPENSHIFT server

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