aseemk / requireDir

Node.js helper to require() directories.
MIT License
484 stars 60 forks source link

Sharing global variable between files possible? #3

Closed xmlking closed 10 years ago

xmlking commented 10 years ago

I have a main file gulpfile.js with variable env initialized.

var gulp = require('gulp');
require('require-dir')('./gulp');

var env = process.env.NODE_ENV  || 'DEV';

gulp.task('default', ['clean'], function () {
    gulp.start('build');
});

I want to use env in ./gulp/build.js

gulp.task('styles', function () {
    return gulp.src('app/styles/main.scss')
        .pipe($.plumber())
        .pipe($.rubySass({
            style: 'expanded',
            precision: 10,
            sourcemap: (env === 'DEV')
        }))
        .pipe($.autoprefixer('last 2 version'))
        .pipe(gulp.dest('.tmp/styles'));
});

may be in some other child file , i want to override env global variable. is it possible ?

aseemk commented 10 years ago

Sorry for the delay here @xmlking!

Unfortunately, no, it's not possible to share a variable across files. This is a limitation (by design) of Node. Your only option would be to move that variable to a shared file/module.

Hope that helps!

NeXTs commented 8 years ago
global.env = process.env.NODE_ENV  || 'DEV';

in other module

console.log(env);
xmlking commented 8 years ago

This works but, order of code execution is not garented. See : https://github.com/lorenwest/node-config/issues/246