nicosantangelo / sublime-gulp

Run Gulp tasks and use snippets from Sublime Text
https://sublime-gulp.nicosantangelo.com/
MIT License
155 stars 18 forks source link

ReferenceError: gulp is not defined (running at Windows) #12

Closed smeijer closed 9 years ago

smeijer commented 10 years ago

I just can't get it working on Windows. Gulp runs fine from command, but sublime-gulp is throwing the error below to it's log. Any idea how to get this working?

# %AppData% = C:\Users\Stephan\AppData\Roaming

%AppData%\Sublime Text 3\Packages\Gulp\write_tasks_to_cache.js:23
        throw ex;
              ^
ReferenceError: gulp is not defined
    at Object.<anonymous> (E:\Develop\Playground\html\.sublime-gulp-tmp.js:12:37)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at requireGulp (%AppData%\Sublime Text 3\Packages\Gulp\write_tasks_to_cache.js:20:16)
    at Object.<anonymous> (%AppData%\Sublime Text 3\Packages\Gulp\write_tasks_to_cache.js:46:12)
    at Module._compile (module.js:456:26)
nicosantangelo commented 10 years ago

Hello!

I had this problem on Windows and usually one of the following fixes it:

"exec_args": {
  "path": "/bin:/usr/bin:/usr/local/bin"
}

Let me know how it goes!

smeijer commented 10 years ago

Gulp is installed globally as well as locally. So that doesn't seem to be the problem.

Any idea what to add specifically to the "path"? My Windows environment path setting seems to be alright, and overriding them trough the config doesn't seem to help.

I've tried the following paths in the exec_args.path property, but they don't fix the "gulp is not defined" error.

nicosantangelo commented 10 years ago

The paths seems to be allright too...let's see.

That error is thrown by the js that gets run by node behind the scenes so the package can get the gulp task list (overcomplicated, I know, but there wasn't a gulp tasks at the time).

So what it does is:

var requireGulp = function(gulpfilePath) {
    // Read your gulpfile.js
    var fileSrc = fs.readFileSync(gulpfilePath);
    // add the exports at the end so it can be required by node
    fileSrc += ";module.exports = gulp;";
    // write the temporal file
    fs.writeFileSync(tmpfilePath, fileSrc);
    try {
        // require the new 'gulpfile', with module.exports = gulp; at the end
        return require(tmpfilePath);
    } catch(ex) {
       // On error, delete the tmp file and throw (the one that gets printed to the log)
        fs.unlink(tmpfilePath);
        throw ex;
    }
};

So!, if you don't mind, try adding module.exports = gulp; to the end of your gulpfile.js and then running the following on your console:

node -e "var gulp = require('./gulpfile.js'); console.log(gulp);"

When I do it, it logs the gulp object to the console

(sorry for the inconvenience)

smeijer commented 9 years ago

Briljant! I've just fixed it. Well, you did actually.

The problem was my way of setting up gulp. My gulpfile.js never required gulp itself. This works, because I separate my tasks into individual js files. My gulpfile.js looks like this:

var fs         = require('fs');
var path       = require('path');
var jsfiles    = function(name) {
    return /(\.(js|coffee)$)/i.test(path.extname(name));
}
var tasks       = fs.readdirSync('./gulp-tasks/').filter(jsfiles);

tasks.forEach(function(task) {
    require('./gulp-tasks/' + task);
});

Appending a require to the file, fixed the problem. Export isn't required.

// require(d) gulp for compatibility with sublime-gulp.
var gulp = require('gulp');
nicosantangelo commented 9 years ago

Wow I never thought of that, it's a good idea.

I think I'll add an explanation to the README that covers this case (and I think I'll start using something similar :P), thanks a lot for letting me know what it was!