简体   繁体   English

在OpenShift上运行npm install --production

[英]Run npm install --production on OpenShift

When I push my code to OpenShift, it looks like it's installing my devDependencies which takes forever . 当我将代码推送到OpenShift时,看起来它正在安装我的devDependencies ,这需要永远 I would really love to set it up so it will only install the dependencies (by running with the --production flag). 我真的很想设置它,所以它只会安装dependencies (通过运行--production标志)。 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 ". 您可以通过将NPM_CONFIG_PRODUCTION环境变量设置为“ true ”来告诉npm使用--production标志进行安装。

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. 对于env var而言(对我来说)的好处是它适用于启动应用程序的所有方式,包括“在OpenShift上启动”按钮。

Create an .openshift/action_hooks/pre_build file: 创建.openshift/action_hooks/pre_build文件:

#!/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. 我已经测试了它确实影响了这个版本的npm,如果你将来删除这个钩子,.npmrc就会消失。

(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.) (很显然,我也可以通过简单地增加一个实现这一.npmrc我的回购,但希望影响人检查出的来源和运行npm install ,它仅仅是如何工作的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. npm install命令位于盒式磁带的bin / control文件夹中。 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. 同时,它已在wshearn / openshift-origin-cartridge-nodejs原始github仓库中修复,因此您只需从github安装而不是使用Quickstart。

  1. Create a .npmrc file where the node_modules folder is located. 创建.npmrc文件夹所在的node_modules文件。

  2. Open it with your text-editor and add this to it: text-editor打开它并将其添加到它:

    production = true

PS no semicolons or any other characters PS没有分号或任何其他字符

This will ensure that devDependencies are not installed on the OPENSHIFT server 这将确保devDependencies都没有安装OPENSHIFT服务器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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