安装后找不到gulp run命令

时间:2016-03-17 07:27:39

标签: javascript npm gulp

我是gulp的新手,我只是创建了一个gulpfile.js。我正在尝试gulp run,但我正在-bash: gulp: command not found

不知道为什么会发生这种情况,因为我已经在本地安装了它。

的package.json:

{
  "name": "taglr",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "webpack --config webpack.config.js && node ./build/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.7.2",
    "babel-loader": "^6.2.4",
    "deepmerge": "^0.2.10",
    "glue": "^3.2.0",
    "hapi": "^13.2.1",
    "jquery": "^2.2.1",
    "react": "^0.14.7",
    "react-dom": "^0.14.7",
    "react-router": "^2.0.1"
  },
  "devDependencies": {
    "babel-preset-es2015": "^6.6.0",
    "babel-preset-react": "^6.5.0",
    "extract-text-webpack-plugin": "^1.0.1",
    "gulp": "^3.9.1",
    "nodemon": "^1.9.1",
    "webpack": "^1.12.14",
    "webpack-dev-server": "^1.14.1"
  }
}

gulpfile.js:

var gulp = require('gulp');
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var DeepMerge = require('deep-merge');
var nodemon = require('nodemon');


// for excluding the building of node_modules in the backend
var nodeModules = {};
fs.readdirSync('node_modules').filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
}).forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
});

// generic config
var defaultConfig = {
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: [
                        'react',
                        'es2015'
                    ]
                }
            }
        ]
    }
};

// if not production build
if (process.env.NODE_ENV !== 'production') {
    defaultConfig.devtool = '#eval-source-map';
    defaultConfig.debug = true
}

// build config using overrides
function buildConfig(config) {
    return DeepMerge(defaultConfig, config || {});
}


var backendConfig = buildConfig({
    entry: path.resolve(__dirname, "server/server.js"),
    // tells webpack not to touch any built-in modules
    target: "node",
    externals: nodeModules,
    output: {
        path: path.resolve(__dirname, "build"),
        filename: 'index.js'    
    },
    pluguns: [

    ]
});

var host = "localhost";
var port = 3000;

var frontendConfig = buildConfig({
    entry: path.resolve(__dirname, "app/index.js"),
    output: {
        path: path.resolve(__dirname, "public/bundle"),
        filename: "main.js"
        // publicPath: 'http://' + host + ':' + port + 'pubic/bundle'
    },
    plugins: [

    ]
});

function onBuild(done) {
    return function(err, stats) {
        if (err) {
            console.log("ERROR: " + err);
        } else {
            console.log(stats.toString());
        }
        if (done) {
            done(); 
        }
    }

}

// build frontend
gulp.task('build-frontend', function(done) {
    webpack(frontendConfig).run(onBuild(done));
});

// watch frontend
gullp.task('watch-frontend', function(done) {
    webpack(frontendConfig).watch(100, onBuild());
});

// build backend
gulp.task('build-backend', function(done) {
    webpack(backendConfig).run(onBuild(done));
});

// watch backend
gullp.task('watch-backend', function(done) {
    webpack(backendConfig).watch(100, function(err, stats) {
        onBuild()(err, stats);
        nodemon.restart();
    });
});


gulp.task('build', ['build-frontend', 'build-backend']);
gulp.task('watch', ['watch-frontend', 'watch-backend']);
gulp.task('run', ['watch-frontend', 'watch-backend'], function() {
    nodemon({
        execMap: {
            js: 'node'
        },

        script: 'build/index.js',
        ext: 'js html'
    })
});

1 个答案:

答案 0 :(得分:2)

问题是在gulp的任何目录中都找不到可执行文件$PATH。如果您在全球范围内安装它,它将被复制到/usr/lib/node_modules$PATH中的某个其他目录(取决于您的发行版),所以只需使用:

npm install -g gulp-cli

或者,如果您无法在该计算机上使用sudo,则可以将其安装到项目包中.json:
npm install --save-dev gulp-cli

链接:
ln -s node_modules/gulp-cli/bin/gulp.js gulp.js

使用本地符号链接执行:
./gulp.js run