Run npm install --production on OpenShift

2019-03-16 00:55发布

问题:

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?

回答1:

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"


回答2:

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



回答3:

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.



回答4:

  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

P.S. no semicolons or any other characters

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