export NODE_ENV =生产不会改变gulpfile.js中的process.env.NODE_ENV

我试图设置环境variables$ NODE_ENV为“生产”

export NODE_ENV=production 

但是当我尝试运行我的吞噬任务时,它说variables是未定义的:

 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) 

如果我echo $NODE_ENV ,则显示生产关键字。

在这个例子中,我的gulp设置如下:

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

如果我正在使用devBuild的完整初始化,它总是如此,因为它不考虑在我的terminal$ NODE_ENV更改

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

有谁知道为什么不在我的大文件中更新variables,我该如何解决这个问题?

在MAC OSX EL Capitan上运行

提前致谢!

后期编辑:

我认为这个错误出现是因为我在使用sudo gulp html;

我使用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('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)) }) 

哪里

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

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

如果我使用gulp html,variables改变生产,但我得到的错误,如果我使用sudo gulp html任务经过,但variables不会改变…

 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

 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 

注意,在这两种情况下,NODE_ENVvariables都是生产…

谢谢

当使用sudo时,你以不同的用户身份运行进程(root)。 这就是为什么环境variables是未定义的。 尝试在你的文件上设置正确的权限,你应该能够没有sudo运行

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

对于错误TypeError: Cannot read property 'trim' of undefined这是因为环境NODE_ENV没有值。

尝试再次检查NODE_ENV。

也可以试试$ NODE_ENV=production gulp [tasks]

经过与SLow Loris的更多调查和讨论,我发现了不同的解决scheme:

  1. 更改错误文件的文件权限:

    错误:EACCES:权限被拒绝,打开“/Users/mmarinescu/Projects/gulpTutorial/build/index.html”

通过写作:

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

这样做的缺点是我需要使用chmod 755来设置权限,我需要在每次运行'gulp html'时再次设置权限

  1. 在项目文件夹中使用sudo su ,然后export NODE_ENV=productionexport NODE_ENV=development并运行export NODE_ENV=development gulp html – 这可以正常运行而不需要再次运行文件权限命令

  2. 删除index.html文件,因为它将被gulp命令重新写入:index.html是从build文件夹创build到生产文件夹的。 你可以通过手动删除它或者通过设置一个吞咽任务来做到这一点, gulp clean ; 你在安装完npm del软件包之后设置了这个任务, sudo npm install del --save-dev ,然后在gulpfile.js

    delModule = require('del'); gulp.task('clean',function(){delModule([dest +'*']); //传入一个delstring数组,在这种情况下,只是包含所有文件的build文件夹)

使用export NODE_ENV =“production”。 如果你的生产variables没有被初始化为一个string,trim不起作用。