Referenceerror gulp is not defined ошибка

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)

you just need to install and require gulp locally, you probably only installed it globally

At the command line

cd <project-root> && npm install --save-dev gulp

In your gulpfile.js

var gulp = require('gulp'); 

this is a different dependency than the command line dependency (that you installed globally). More specifically, it is the same NPM package, but the command line program will execute code usually from a different entry point in the NPM package then what require(‘X’) will return.

If we go to the package.json file in the Gulp project on Github, it will tell the whole story:

{
  "name": "gulp",
  "description": "The streaming build system",
  "version": "3.9.1",
  "homepage": "http://gulpjs.com",
  "repository": "gulpjs/gulp",
  "author": "Fractal <contact@wearefractal.com> (http://wearefractal.com/)",
  "tags": [ ],
  "files": [ 
     // ...
   ],
  "bin": {
    "gulp": "./bin/gulp.js"
  },
  "man": "gulp.1",
  "dependencies": {
    // ...
  },
  "devDependencies": {
     // ...
  },
  "scripts": {
    "prepublish": "marked-man --name gulp docs/CLI.md > gulp.1",
    "lint": "eslint . && jscs *.js bin/ lib/ test/",
    "pretest": "npm run lint",
  },
  "engines": {
    "node": ">= 0.9"
  },
  "license": "MIT"
}

so at the command line:

$ gulp default

will execute this:

     "bin": {
        "gulp": "./bin/gulp.js"
      },

on the other hand, require(‘gulp’) in your code will return the value of this:

https://github.com/gulpjs/gulp/blob/master/index.js

normally we see this in a package.json file as:

"main": "index.js"

but since this is the default, they just omitted it (which is dumb IMO, better to be explicit, but they aren’t the first project I have seen take the lame shorthand route.).

При запуске gulp из ST3 получаю вот такой лог

Remember that you can report errors and get help in https://github.com/NicoSantangelo/sublime-gulp

05-08-2018 18:54:
C:Users***AppDataRoamingSublime Text 3PackagesGulpwrite_tasks_to_cache.js:31
        throw ex;
        ^

ReferenceError: gulp is not defined
    at Object.<anonymous> (D:!!!Study!gulp4template.sublime-gulp-tmp.js:31:23)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at requireGulp (C:Users***AppDataRoamingSublime Text 3PackagesGulpwrite_tasks_to_cache.js:28:16)
    at Object.<anonymous> (C:Users***AppDataRoamingSublime Text 3PackagesGulpwrite_tasks_to_cache.js:83:12)


05-08-2018 18:57:
C:Users***AppDataRoamingSublime Text 3PackagesGulpwrite_tasks_to_cache.js:31
        throw ex;
        ^

ReferenceError: gulp is not defined
    at Object.<anonymous> (D:!!!Study!gulp4template.sublime-gulp-tmp.js:31:23)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at requireGulp (C:Users**AppDataRoamingSublime Text 3PackagesGulpwrite_tasks_to_cache.js:28:16)
    at Object.<anonymous> (C:Users***AppDataRoamingSublime Text 3PackagesGulpwrite_tasks_to_cache.js:83:12)

Вот gulfile.js:

global.$ = {
    path: {
        task: require('./gulp/paths/tasks.js')
    },
    gulp: require('gulp'),
    del: require('del'),
    fs: require('fs'),
    browserSync: require('browser-sync').create(),
    gp: require('gulp-load-plugins')()
};

$.path.task.forEach(function(taskPath) {
    require(taskPath)();
});

$.gulp.task('dev', $.gulp.series(
    'clean',
    $.gulp.parallel('styles:dev', 'pug', 'libsJS:dev', 'js:copy', 'svg', 'img:dev', 'fonts','svg:copy')));

$.gulp.task('build', $.gulp.series(
    'clean',
    $.gulp.parallel('styles:build', 'pug', 'libsJS:build', 'js:copy', 'svg', 'img:build', 'fonts','svg:copy')));

$.gulp.task('default', $.gulp.series(
    'dev',
    $.gulp.parallel(
        'watch',
        'serve'
    )
));

Версия галпа CLI version 2.0.1
Local version 4.0.0-alpha.2
В чем дело, подскажите плиз.

First, let’s make sure we have node and npm installed:

$ node -v
v0.10.25

$ npm -v
1.3.24

Now let’s try to run gulp. If everything goes to plan, we should see our first error:

$ gulp
command not found: gulp

Perfect! Let’s ask NPM for a fresh copy of gulp:

$ npm install gulp -g
... installing gulp ...

This installs gulp command-line utility and makes available globally. Now, let’s try running gulp again:

$ gulp
[gulp] No gulpfile found

Nice, our first error!

gulp is telling us that it doesn’t have a gulpfile with instructions for it to follow. We solve this by simply adding gulpfile.js to our directory root:

We haven’t added any instructions but sipmly having having the file should give us a new error:

$ gulp
[gulp] Using gulpfile /Users/chan/code/talks/gulpfile.js
[gulp] Task 'default' was not defined in your gulpfile but you tried to run it.
[gulp] Please check the documentation for proper gulpfile formatting.

Boom! Now we’re talking. gulp is running and it sees our gulpfile. We’re on a roll!

It tells us that we don’t have a default task. Every gulpfile needs a default task to run, when gulp is executed without arguments.

We find the syntax for creating a gulp task in their docs. Looks like it’s gulp.task('taskName');. Let’s try it:

$ echo "gulp.task('default');" >> gulpfile.js

$gulp
... ReferenceError: gulp is not defined ...

Yuck! Now we have a really ugly error…

But don’t lose heart. This error looks scarier than it is.

node is telling us that there is no reference to ‘gulp’ in our file. We simply need to add a reference to gulp in our gulpfile:

[ add the following line to the very top of your gulpfile ]

// gulpfile.js

var gulp = require('gulp');

Now that we’ve told node what gulp means, in our script, lets try running gulp again:

$ gulp
[gulp] Using gulpfile /Users/chan/code/talks/gulpfile.js
[gulp] Starting 'default'...
[gulp] Finished 'default' after 71 μs

Hurray! We now have a working gulpfile that does nothing! Pat yourself on the back firmly.


Adding a process

Now, gulp just sits on top of node. So, we can do anything we could do node:

// gulpfile.js

// update our default task with a function
gulp.task('default', function () { console.log('Hello Gulp!') });

Run this and we’ll see Hello Gulp! amidst the gulp output:

$ gulp
[gulp] Using gulpfile /Users/chan/code/talks/gulpfile.js
[gulp] Starting 'default'...
Hello Full Stack
[gulp] Finished 'default' after 137 μs

This is particularly unuseful. So, let’s turn this into something that can compile CoffeeScript into JavaScript.

Let’s start with a new task for this: gulp scripts.

We’re going to mimic what we did with our default task. gulp gives great errors. It’s easy to start with the process you want and respond to the errers until it works.

Let’s try gulp scripts

$ gulp scripts
[gulp] Using gulpfile /Users/chan/code/talks/gulpfile.js
[gulp] Task 'scripts' was not defined in your gulpfile but you tried to run it.
[gulp] Please check the documentation for proper gulpfile formatting.

As I’m sure you expected, this fails. We haven’t written a scripts task. Let’s write it now.

[ add this to your gulpfile ]

// gulpfile.js
gulp.task('scripts', function () {
   gulp.src('src/*.coffee').pipe(gulp.dest('./'));
});

There’s a lot of code we haven’t covered here. So take a second to look it over. It may seem jarring at first but give it a second to sink it…

Now we can talk about what it’s doing.

First, we’ve registered a new task named scripts. Much like our default task, it responds by running a function.

That function start by calling gulp.src() to collect the files in the path we specify. Here, we’re using * to say “grab any and all files in the src directory.

We then use pipe() to funnel those files through another process, gulp.dest().

gulp.dest() places the files specified with a path. In example, we’re using ./ to tell gulp that we want our finished files dumped into the root directory.

Notice how easily that reads? If you’ve been using JavaScript for a while, this should look painfully obvious.

Any ideas what will happen when we run this?

[ this section requires some previous setup. Add file src/fun.coffee for the examples to work ]

Gulp found our src/fun.coffee file and processed it by creating a new fun.coffee file in the directory root.

Great! But this isn’t exactly what we want. All we’ve done is duplicate our file in the directory root.

We need to process the CoffeScript file before placing it in the destination path.

We can accomplish this by chaining another pipe() right between gulp.src() and .pipe(gulp.dest(...)). This pipe will handle the actual processing of the file.

[ update the task to look like this ]

// gulpfile.js
gulp.task('scripts', function () {
   gulp.src('src/*.coffee')
    .pipe(coffee())
    .pipe(gulp.dest('./'));
});

Now, when we run this, you shold have a pretty good idea what’s going to happen:

$ gulp scripts
[gulp] Using gulpfile /Users/chan/code/talks/gulpfile.js
[gulp] Starting 'scripts'...
[gulp] 'scripts' errored after 5.17 ms coffee is not defined

That’s right. Another error!

You’ll notice that gulp is getting smarter and our errors are less scary. gulp tells us that we have another reference error. We need a coffee processor. Let’s add the reference to our gulpfile:

// gulpfile
var coffee = require('gulp-coffee');

Now it should work, right?!

$ gulp scripts

Error: Cannot find module 'gulp-coffee'

AHHH! Our error is getting uglier! We have our reference but node can’t find gulp-coffee.

To add it. We turn back to our trusty package manager, NPM.

$ npm install gulp-coffee
... installing gulp-coffee ...

And now, running gulp scripts, we should get a fun.js file placed in our root directory:

$ gulp scripts
$ ls
fun.js gulpfile.js src

“And there was much reqoicing.”


If there is time…

It’s worth pointing out that this isn’t any better than using the coffee cli. If we have to run the gulp command each time we change our file, howe does gulp help us?

We need a way to watch the file system. And gulp.watch() helps us with exactly that.

Like gulp.task(), gulp.watch() can take a path as it’s first argument and a function as its second. But when we watch a directory, we may want to run a number of tasks!

gulp accounts for this by allowing you to send an array of task names as the second argument. Let’s create a new task watch to play with this:

// gulpfile.js
gulp.task('watch', function () {
  gulp.watch('src/*.coffee', ['scripts']);
});

Now, running gulp watch will continuously watch our file system for changes:

$ gulp watch

[ make changes to src/fun.coffee ]

Play around with src/fun.coffee and see our watch task respond in the terminal.

// fun.coffee
console.log 'Hello Gulp!'

You can see that gulp is keeping an open process to re-run our task each time fun.coffee is changed. AWESOME!


Changing the default task

Sadly, we only have one task. It’d be nice to have that be our default wouldn’t it? Running gulp without arguments would save us a lot of finger fatigue.

Let’s update our default task to run scripts and watch when gulp is run without arguments.

// gulpfile.js
gulp.task('default', ['scripts', 'watch']);

🙂

outro

As you can see, Gulp.js is insanely flexible, has very little configuration, and writes like application code.

I think you’ll really like using it in your projects.

Recommend Projects

  • React photo

    React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo

    Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo

    Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo

    TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo

    Django

    The Web framework for perfectionists with deadlines.

  • Laravel photo

    Laravel

    A PHP framework for web artisans

  • D3 photo

    D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Visualization

    Some thing interesting about visualization, use data art

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo

    Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo

    Microsoft

    Open source projects and samples from Microsoft.

  • Google photo

    Google

    Google ❤️ Open Source for everyone.

  • Alibaba photo

    Alibaba

    Alibaba Open Source for everyone

  • D3 photo

    D3

    Data-Driven Documents codes.

  • Tencent photo

    Tencent

    China tencent open source team.

Понравилась статья? Поделить с друзьями:
  • Reduced engine performance range rover ошибка
  • Reduce engine performance land rover ошибка
  • Redmond робот пылесос ошибки e000
  • Robocopy ошибка 5 0x00000005 изменение атрибутов файла
  • Rm невозможно удалить ошибка ввода вывода