I'm trying to set the environment variable $NODE\_ENV to "production" with 

```
export NODE_ENV=production

```

but when I try to run my gulp task, it says the variable is undefined:

```
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.

My gulp setup in this example is as it follows:

```
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

```
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? 

Running on a MAC OSX EL Capitan

Thanks in advance!

LATER EDIT:

I think the error appears because I'm using sudo gulp html;

The reason I'm using sudo is because when I'm using gulp html, I get this error: 

```
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.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...

```
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

```

NOTE, the NODE\_ENV variable is production in both cases...

Thanks