Gulp Concat JS results in require is not defined

两盒软妹~` 提交于 2019-12-24 00:46:04

问题


I have project using gulp.

I am trying to get jquery, jquery ui and bootstrap into one file called vendor.js.

This works fine however when I run "gulp serve" to run the project fromthe dist folder of the project i get the error

ReferenceError: require is not defined http://localhost:3000/assets/toolkit/scripts/vendor.js Line 6

var jQuery = require('jquery');

gulp.task('vendor-js', function() {
       return gulp.src(['src/assets/toolkit/scripts/vendor/jquery.min.js', 
       'src/assets/toolkit/scripts/vendor/jquery-ui.js', 
       'src/assets/toolkit/scripts/bootstrap/bootstrap.min.js' ])
       .pipe(concat('vendor.js'))
       .pipe(gulp.dest('dist/assets/toolkit/scripts/'));
}); 

Folder Structure

dist\assets\toolkit\scripts

dist\assets\toolkit\styles

dist\assets\toolkit\images

dist\pages

index.html

Why is this the case? I have looked all over with no luck! I have seen similar issues but all relating to react?

Can anyone help?

Thanks

NIck


回答1:


Managed to get this working eventually so I thought i would post my solution.

I reverted to using the built in toolkit.js file to require each dependency. In this case Jquery, Jquery Ui, Bootstrap Sass and Chart Js.

In my toolkit.js file i included

window.$ = window.jQuery = require('jquery'); require('jquery-ui'); require('bootstrap-sass'); require('chart.js');

This is then concatenated by my Gulp task.

The key is window.$ = window.jQuery = require('jquery');

rather than

require('jquery');


来源:https://stackoverflow.com/questions/37069465/gulp-concat-js-results-in-require-is-not-defined

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