出口 NODE_ENV=生产在 gulpfile.js 中没有改变 process.env.NODE_ENV
export NODE_ENV=production doesn't change process.env.NODE_ENV in gulpfile.js
我正在尝试使用
将环境变量 $NODE_ENV 设置为 "production"
export NODE_ENV=production
但是当我尝试 运行 我的 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)
如果我echo $NODE_ENV
,但是,它显示生产关键字。
本例中我的gulp设置如下:
var devBuild = ((process.env.NODE_ENV).trim().toLowerCase() !== 'production')
如果我使用的是 devBuild 的完整初始化,它总是正确的,因为它没有考虑我终端中的 $NODE_ENV 变化
var devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() !== 'production')
有谁知道为什么我的 gulp 文件中的变量没有更新,我该如何解决这个问题?
运行 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任务是:
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,变量更改为生产,但我得到错误,如果我使用 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)
对
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_ENV 变量在两种情况下都是生产...
谢谢
使用导出 NODE_ENV = "production"。如果您的生产变量未初始化为字符串,trim 对其不起作用。
我想这段代码没有问题 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]
使用 sudo 时,您 运行 以不同的用户 (root) 身份运行该进程。这就是环境变量未定义的原因。尝试为您的文件设置正确的权限,您应该可以 运行 而无需 sudo
经过与 SLow Loris 的更多调查和讨论,我找到了不同的解决方案:
更改错误文件的文件权限:
错误:EACCES:权限被拒绝,打开'/Users/mmarinescu/Projects/gulpTutorial/build/index.html'
通过写作:
chmod 755 /Users/mmarinescu/Projects/gulpTutorial/build/index.html
这样做的缺点是我需要使用 chmod 755 设置权限,我需要在每次 运行ning 'gulp html'
时再次设置权限
在项目文件夹中使用 sudo su
,然后使用 export NODE_ENV=production
或 export NODE_ENV=development
和 运行 gulp html
- 这在没有 运行再一次文件权限命令
删除index.html文件,因为它将被gulp命令再次重写:index.html是从构建文件夹创建到生产一。您可以通过手动删除它或设置 gulp 任务 gulp clean
来完成此操作;您在安装 npm del 包后设置此任务,sudo npm install del --save-dev
,然后在 gulpfile.js
delModule = require('del');
gulp.task('clean', 函数 () {
delModule([dest + '*']); //传递给 del 一个字符串数组,在这种情况下,只是包含所有文件的构建文件夹
})
我正在尝试使用
将环境变量 $NODE_ENV 设置为 "production"export NODE_ENV=production
但是当我尝试 运行 我的 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)
如果我echo $NODE_ENV
,但是,它显示生产关键字。
本例中我的gulp设置如下:
var devBuild = ((process.env.NODE_ENV).trim().toLowerCase() !== 'production')
如果我使用的是 devBuild 的完整初始化,它总是正确的,因为它没有考虑我终端中的 $NODE_ENV 变化
var devBuild = ((process.env.NODE_ENV || 'development').trim().toLowerCase() !== 'production')
有谁知道为什么我的 gulp 文件中的变量没有更新,我该如何解决这个问题?
运行 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任务是:
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,变量更改为生产,但我得到错误,如果我使用 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)
对
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_ENV 变量在两种情况下都是生产...
谢谢
使用导出 NODE_ENV = "production"。如果您的生产变量未初始化为字符串,trim 对其不起作用。
我想这段代码没有问题 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]
使用 sudo 时,您 运行 以不同的用户 (root) 身份运行该进程。这就是环境变量未定义的原因。尝试为您的文件设置正确的权限,您应该可以 运行 而无需 sudo
经过与 SLow Loris 的更多调查和讨论,我找到了不同的解决方案:
更改错误文件的文件权限:
错误:EACCES:权限被拒绝,打开'/Users/mmarinescu/Projects/gulpTutorial/build/index.html'
通过写作:
chmod 755 /Users/mmarinescu/Projects/gulpTutorial/build/index.html
这样做的缺点是我需要使用 chmod 755 设置权限,我需要在每次 运行ning 'gulp html'
时再次设置权限在项目文件夹中使用
sudo su
,然后使用export NODE_ENV=production
或export NODE_ENV=development
和 运行gulp html
- 这在没有 运行再一次文件权限命令删除index.html文件,因为它将被gulp命令再次重写:index.html是从构建文件夹创建到生产一。您可以通过手动删除它或设置 gulp 任务
gulp clean
来完成此操作;您在安装 npm del 包后设置此任务,sudo npm install del --save-dev
,然后在 gulpfile.jsdelModule = require('del'); gulp.task('clean', 函数 () { delModule([dest + '*']); //传递给 del 一个字符串数组,在这种情况下,只是包含所有文件的构建文件夹 })