【问题标题】:export NODE_ENV=production doesn't change process.env.NODE_ENV in gulpfile.jsexport NODE_ENV=production 不会改变 gulpfile.js 中的 process.env.NODE_ENV
【发布时间】:2015-11-26 22:13:23
【问题描述】:

我正在尝试使用

将环境变量 $NODE_ENV 设置为“生产”
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')

有谁知道为什么我的 gulpfile 中的变量没有更新,我该如何解决这个问题?

在 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.js macos gulp environment-variables


    【解决方案1】:

    使用 sudo 时,您以不同的用户 (root) 身份运行该进程。这就是环境变量未定义的原因。尝试为您的文件设置正确的权限,您应该能够在没有 sudo 的情况下运行

    【讨论】:

      【解决方案2】:

      我想这段代码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]

      【讨论】:

      • 不...这不能解决它。再次更新了我的问题
      【解决方案3】:

      经过与SLow Loris的更多调查和讨论,我找到了不同的解决方案:

      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=production 或 export NODE_ENV=development 并运行 gulp html - 无需再次运行文件权限命令即可正常工作

      2. 删除 index.html 文件,因为它将被 gulp 命令再次重写:index.html 是从构建文件夹创建到生产文件夹。你可以通过手动删除它或设置一个 gulp 任务来做到这一点,gulp clean;你在安装 npm del 包sudo npm install del --save-dev,然后在 gulpfile.js 中设置这个任务

        delModule = 要求('del'); gulp.task('clean', function () { delModule([dest + '*']); //将一个字符串数组传递给del,在这种情况下,只是包含所有文件的构建文件夹 })

      【讨论】:

      • 不知道为什么,但是我的mac对我的文件夹只有读取权限,所以我按照本教程中的步骤添加读/写权限:support.apple.com/kb/PH18894?locale=en_US
      • 为了避免以后不得不处理文件权限,您可以稍微修改您的设置。我找到了一个关于这个主题的非常好的教程:How to use npm global without sudo on OSX 然而,这意味着你必须重新安装你的全局包,但你永远不必再做sudo npm install --global ***。
      • 感谢您的帮助!事实证明,主要问题来自我一直在使用的文件,它们只有读取权限......设法将这些文件的权限设置为读取和写入,现在我可以使用 NODE_ENV 了,没有需要使用sudo或sudo su等
      【解决方案4】:

      使用 export NODE_ENV = "生产"。如果您的生产变量未初始化为字符串,则 trim 不起作用。

      【讨论】:

      • 这不能解决问题,用额外的信息更新了我的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2023-03-15
      • 2015-06-20
      • 1970-01-01
      • 2013-10-29
      • 2014-01-16
      • 2012-03-04
      相关资源
      最近更新 更多