简体   繁体   English

export NODE_ENV = production不会更改gulpfile.js中的process.env.NODE_ENV

[英]export NODE_ENV=production doesn't change process.env.NODE_ENV in gulpfile.js

I'm trying to set the environment variable $NODE_ENV to "production" with 我正在尝试将环境变量$ NODE_ENV设置为“production”

export NODE_ENV=production

but when I try to run my gulp task, it says the variable is undefined: 但是当我尝试运行我的gulp任务时,它说变量是未定义的:

if((process.env.NODE_ENV).trim().toLowerCase() !== 'production') {
                     ^

TypeError: Cannot read property 'trim' of undefined
at Object.<anonymous> (/Users/mmarinescu/Projects/gulpTutorial/gulpfile.js:15:26)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Liftoff.handleArguments (/usr/local/lib/node_modules/gulp/bin/gulp.js:116:3)
at Liftoff.<anonymous> (/usr/local/lib/node_modules/gulp/node_modules/liftoff/index.js:192:16)
at module.exports (/usr/local/lib/node_modules/gulp/node_modules/liftoff/node_modules/flagged-respawn/index.js:17:3)

If I echo $NODE_ENV , however, it shows the production keyword. 但是,如果我echo $NODE_ENV ,它会显示production关键字。

My gulp setup in this example is as it follows: 我在这个例子中的gulp设置如下:

var devBuild = ((process.env.NODE_ENV).trim().toLowerCase() !== 'production')

If I'm using the full initialisation of devBuild, it is always true, as it doesn't consider the $NODE_ENV change in my terminal 如果我正在使用devBuild的完整初始化,它总是正确的,因为它不考虑终端中的$ NODE_ENV更改

var devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() !== 'production')

Does anyone know why doesn't the variable update in my gulpfile and how can I fix this? 有谁知道为什么变量不在我的gulpfile中更新,我该如何解决这个问题?

Running on a MAC OSX EL Capitan 在MAC OSX EL Capitan上运行

Thanks in advance! 提前致谢!

LATER EDIT: 后期编辑:

I think the error appears because I'm using sudo gulp html; 我认为错误出现是因为我正在使用sudo gulp html;

The reason I'm using sudo is because when I'm using gulp html, I get this error: 我使用sudo的原因是因为当我使用gulp html时,我收到此错误:

gulptutorial 1.0.0, production build
[00:44:21] Using gulpfile ~/Projects/gulpTutorial/gulpfile.js
[00:44:21] Starting 'html'...
[00:44:22] 'html' errored after 101 ms
[00:44:22] Error: EACCES: permission denied, open      '/Users/mmarinescu/Projects/gulpTutorial/build/index.html'
at Error (native)

gulp task is: gulp任务是:

gulp.task('html', function() {
var page = gulp.src(html.in).pipe(preprocess({ context: html.context }));//we add the preprocessed html to a var
if(!devBuild) { //global variable defined on step 7
    page = page.pipe(htmlclean());//minifying the html only if we're in the production mode

    console.log(devBuild)
}
return page.pipe(gulp.dest(html.out))
})

where 哪里

html = {
    in: source + '*.html',
    out: dest,
    context: {
        devBuild: devBuild
    }
};

and

var source = 'source/',
dest = 'build/';

If I use gulp html, the variable changes to production, but I get the error, if I'm using sudo gulp html the task goes through, but the variable doesn't change... 如果我使用gulp html,变量会更改为生产,但我得到错误,如果我使用sudo gulp html任务通过,但变量不会改变...

gulpTutorial mmarinescu$ export   NODE_ENV=production
gulpTutorial mmarinescu$  gulp html
gulptutorial 1.0.0, production build
[00:58:12] Using gulpfile ~/Projects/gulpTutorial/gulpfile.js
[00:58:12] Starting 'html'...
[00:58:12] 'html' errored after 87 ms
[00:58:12] Error: EACCES: permission denied, open '/Users/mmarinescu/Projects/gulpTutorial/build/index.html'
at Error (native)

vs VS

gulpTutorial mmarinescu$ sudo gulp html
gulptutorial 1.0.0, development build
[01:02:10] Using gulpfile ~/Projects/gulpTutorial/gulpfile.js
[01:02:10] Starting 'html'...
[01:02:10] Finished 'html' after 67 ms

NOTE, the NODE_ENV variable is production in both cases... 注意,NODE_ENV变量在两种情况下都是生产...

Thanks 谢谢

I guess there is no problem with this code if((process.env.NODE_ENV).trim().toLowerCase() !== 'production') { but better you discard brackets if(process.env.NODE_ENV.trim().toLowerCase() !== 'production') { 我想这个代码没有问题if((process.env.NODE_ENV).trim().toLowerCase() !== 'production') {但你最好丢弃括号if(process.env.NODE_ENV.trim().toLowerCase() !== 'production') {

for the error TypeError: Cannot read property 'trim' of undefined it is because environment NODE_ENV there is no value. 对于错误TypeError: Cannot read property 'trim' of undefined因为环境NODE_ENV没有值。

try to check again on NODE_ENV. 尝试再次检查NODE_ENV。

also try check with $ NODE_ENV=production gulp [tasks] 还试试用$ NODE_ENV=production gulp [tasks]

When using sudo you are running the process as a different user (root). 使用sudo时,您将以不同的用户(root)身份运行该进程。 That is why environment variables are undefined. 这就是环境变量未定义的原因。 Try setting the right permissions on your files and you should be able to run without sudo 尝试为您的文件设置正确的权限,您应该能够在没有sudo的情况下运行

After more investigation and discussion with SLow Loris, I found different solutions: 经过与SLow Loris的更多调查和讨论,我发现了不同的解决方案:

  1. Change the file permissions of the error file: 更改错误文件的文件权限:

    Error: EACCES: permission denied, open '/Users/mmarinescu/Projects/gulpTutorial/build/index.html' 错误:EACCES:权限被拒绝,打开'/Users/mmarinescu/Projects/gulpTutorial/build/index.html'

by writing: 写作:

chmod 755 /Users/mmarinescu/Projects/gulpTutorial/build/index.html

The downside to this is that I need to set the permission with chmod 755, I need to set the permission again each time I'm running 'gulp html' 这方面的缺点是我需要使用chmod 755设置权限,每次运行'gulp html'时我需要再次设置权限

  1. use sudo su in the project folder, then export NODE_ENV=production or export NODE_ENV=development and run gulp html - this works okay without running again the file permissions command 在项目文件夹中使用sudo su ,然后export NODE_ENV=productionexport NODE_ENV=development并运行export NODE_ENV=development gulp html - 这样可以正常运行而不再运行文件权限命令

  2. delete the index.html file, as it will be re-written again by the gulp command: the index.html is created from the build folder to the production one. 删除index.html文件,因为它将由gulp命令重新编写:index.html从build文件夹创建到生产文件夹。 you can do this either by deleting it manually or by setting up a gulp task, gulp clean ; 您可以通过手动删除或通过设置gulp任务来完成此操作, gulp clean ; you set up this task after installing the npm del package, sudo npm install del --save-dev , then in gulpfile.js 你在安装了npm del包之后设置了这个任务, sudo npm install del --save-dev ,然后在gulpfile.js中

    delModule = require('del'); delModule = require('del'); gulp.task('clean', function () { delModule([dest + '*']); //passing into del an array of strings, in this case, just the build folder with all it's files }) gulp.task('clean',function(){delModule([dest +'*']); //传入del一个字符串数组,在这种情况下,只是构建文件夹及其所有文件})

use export NODE_ENV = "production". 使用export NODE_ENV =“production”。 if your production variable is not initialized as a string, trim doesn't work on it. 如果您的生产变量未初始化为字符串,则修剪不起作用。

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

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