Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
smeijer opened this issue Sep 14, 2014 · 5 comments
Closed

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

smeijer opened this issue Sep 14, 2014 · 5 comments

Comments

@smeijer
Copy link

smeijer commented Sep 14, 2014

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
Copy link
Owner

Hello!

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

  • If you didn't, run npm install gulp on the project folder, even if you have gulp installed globally.
  • Normally, It isn't a problem on Windows, but it could be a issue with the PATH. The package will try to get the PATH from the environment, but you can override it by adding exec_args to your gulp settings. For example, on Linux I have:
"exec_args": {
  "path": "/bin:/usr/bin:/usr/local/bin"
}

Let me know how it goes!

@smeijer
Copy link
Author

smeijer commented Sep 14, 2014

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.

  • C:/Users/Stephan/AppData/Roaming/npm
  • C:/Users/Stephan/AppData/Roaming/npm/node_modules
  • C:/Program Files (x86)/nodejs
  • E:/Develop/Playground/html/node_modules

@nicosantangelo
Copy link
Owner

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
Copy link
Author

smeijer commented Sep 15, 2014

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');

@smeijer smeijer closed this as completed Sep 15, 2014
@nicosantangelo
Copy link
Owner

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!

erik-smit pushed a commit to erik-smit/gulp-screeps that referenced this issue Nov 29, 2016
Without this, I get the follow error:

```
C:\Users\zoiah\Documents\dev\gulpfile.js:4
gulp.task('screeps', function() {
^

ReferenceError: gulp is not defined
```

Found the fix on nicosantangelo/sublime-gulp#12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants