Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GULP: gulp is not defined

Tags:

npm

gulp

As shown in the screen shot below I am not able to run gulp to concat the JavaScript files. Its saying that gulp is not defined.

I have tried the following commands:

npm install -g gulp
npm install gulp
npm install gulp --save-dev

I have also set the environment variables as following:

C:\Users\<user>\AppData\Roaming\npm;C:\Python27;C:\Users\<user>\AppData\Roaming\npm\node_modules;C:\Users\<user>\AppData\Roaming\npm\node_modules\gulp;

enter image description here

var concat = require('gulp-concat');  
var rename = require('gulp-rename');  
var uglify = require('gulp-uglify');  


//script paths
var jsFiles = 'scripts/*.js',  
    jsDest = 'dist/scripts';

gulp.task('scripts', function() {  
    return gulp.src(jsFiles)
        .pipe(concat('scripts.js'))
        .pipe(gulp.dest(jsDest));
});
like image 205
Vikas Bansal Avatar asked Sep 19 '16 07:09

Vikas Bansal


People also ask

Why is gulp not recognized?

Or: The term 'gulp' is not recognized as the name of a cmdlet, function, script file, or operable program. This usually means that you need to install the Gulp CLI globally on your computer and check that npm is added to your Path environment variables.

How do I know if gulp is installed?

First run npm -g install gulp-cli then run gulp -v. Alternatively, you can just run npm list gulp.

How do I install gulp globally?

To install the Gulp CLI globally, on your command line, run npm install gulp-cli -g . The -g flag means npm will install the package in the global npm directory, so you can run the gulp command from any directory.


1 Answers

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 <[email protected]> (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.).

like image 71
Alexander Mills Avatar answered Oct 05 '22 02:10

Alexander Mills