【问题标题】:gulp run command not found after installing安装后找不到 gulp 运行命令
【发布时间】:2020-05-10 13:38:29
【问题描述】:

我是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'
    })
});

【问题讨论】:

  • 您是否以run.. 的名称定义了任何任务?
  • @MoidMohd 是的,我做到了。让我更新!
  • 哦...我猜你只是在本地安装了 gulp,这可能是问题...npm install --global gulp-cli
  • @MoidMohd 成功了!!谢谢你!你可以回答这个问题,我会接受!
  • 不...它很好..你得到它的工作就是这样.. :)

标签: javascript npm gulp


【解决方案1】:

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

npm install -g gulp-cli

或者如果你不能在那台机器上使用sudo,你可以将它安装到你的项目package.json中:
npm install --save-dev gulp-cli

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

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

【讨论】:

    猜你喜欢
    • 2014-04-09
    • 2014-09-25
    • 2014-07-24
    • 2016-06-09
    • 1970-01-01
    • 2016-04-10
    • 2018-03-10
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多