$ Is not defined scripts Gulp task

我的梦境 提交于 2019-12-24 17:24:31

问题


Hi I am trying to run my scripts task in gulp. My scripts path is defined as follows:

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

My original file paths are located in root/src/js/ and I want my scripts to be placed in their minified version inside root/app/assets/scripts/.

My gulp task is as follows:

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

UPDATE

Hey I think you are both right. $ was not defined and should be $ = require('gulp-load-plugins')();. However now that I am running scripts, I get the following error:

[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

Again, another vague error "undefined is not a function", is this explicit? Is there a $ function missing?


回答1:


You probably need to use gulp-load-plugins package:

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


来源:https://stackoverflow.com/questions/32353432/is-not-defined-scripts-gulp-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!