$未定义脚本Gulp任务

时间:2015-09-02 12:39:52

标签: node.js gulp

您好我正试图在gulp中运行我的脚本任务。我的脚本路径定义如下:

scripts: {
        coffee: [
            'app/assets/scripts/**/*.coffee'
        ],
        js: [
            'src/js/*',
            'src/js/**/*'
        ],
        map: ['src/js/**/*.map']
    },

我的原始文件路径位于root / src / js /中,我希望我的脚本放在root / app / assets / scripts /中的缩小版本中。

我的gulp任务如下:

gulp.task('scripts', function() {
    return gulp.src('app/scripts/**/*.js')
        .pipe($.if('*.js', $.uglify({preserveComments: 'some'})))
        .pipe($.sourcemaps.init())
        .pipe($.concat('app.js'))
        .pipe($.sourcemaps.write('./'))
        .pipe(gulp.dest('app/assets/scripts'));
});

But for some reason I keep getting this error
[13:28:46] ReferenceError: $ is not defined
    at Gulp.<anonymous> (C:\Users\Myuser\apps\myapp\gulpfile.js:165:9)
    at module.exports (C:\Users\Myuser\apps\myapp\node_modules\gulp\node_
modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (C:\Users\Myuser\apps\myapp\node_modules\g
ulp\node_modules\orchestrator\index.js:134:8)
    at C:\Users\Myuser\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:2
0
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
    at startup (node.js:129:16)
    at node.js:814:3

更新

嘿,我觉得你们都是对的。 $未定义,应为$ = require('gulp-load-plugins')();。但是现在我正在运行scripts,我收到以下错误:

[15:24:46] TypeError: undefined is not a function
    at Gulp.<anonymous> (C:\Users\Myuser\apps\myapp\gulpfile.js:166:13)
    at module.exports (C:\Users\Myuser\apps\myapp\node_modules\gulp\node_
modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (C:\Users\Myuser\apps\myapp\node_modules\g
ulp\node_modules\orchestrator\index.js:134:8)
    at C:\Users\Myuser\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:2
0
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
    at startup (node.js:129:16)
    at node.js:814:3

同样,另一个含糊的错误“未定义不是函数”,这是明确的吗?是否缺少$函数?

1 个答案:

答案 0 :(得分:3)

您可能需要使用gulp-load-plugins包:

var $ = require('gulp-load-plugins')({lazy: true});