I'm working on a ruby on rails project and am using Browserify to manage javascript dependencies etc. However, when I run the application I keep getting a "Uncaught ReferenceError: jQuery is not defined" because jquery-ujs tries to call jQuery but it's not available. I know jQuery is being loaded on page because when I include the code below without import 'jquery-ujs' I am able to successfully use jQuery by calling both $ and jQuery. However, I can't seem to be able to also include 'jquery-ujs'.
import 'jquery';
import $ from 'jquery';
global.jQuery = $;
import 'jquery-ujs'
And here is my package.json file:
{
"name": "frontend",
"version": "1.0.0",
"private": true,
"scripts": {
"watchify": "watchify -vd -p browserify-hmr -e frontend/application.js -o app/assets/javascripts/application.js",
"build": "cross-env NODE_ENV=production browserify frontend/application.js | uglifyjs -c warnings=false -m > app/assets/javascripts/application.js"
},
"dependencies": {
"jquery": "^2.2.4",
"jquery-ujs": "^1.2.2",
"vue": "^1.0.0",
"vue-resource": "^0.7.0"
},
"devDependencies": {
"babel-core": "^6.0.0",
"babel-plugin-transform-runtime": "^6.0.0",
"babel-preset-es2015": "^6.0.0",
"babel-preset-stage-2": "^6.0.0",
"babel-runtime": "^6.0.0",
"babelify": "^7.2.0",
"browserify": "^12.0.1",
"browserify-hmr": "^0.3.1",
"browserify-shim": "^3.8.12",
"cross-env": "^1.0.6",
"uglify-js": "^2.5.0",
"vue-hot-reload-api": "^1.2.2",
"vueify": "^8.0.0",
"vueify-insert-css": "^1.0.0",
"watchify": "^3.4.0"
},
"browserify": {
"transform": [
"vueify",
"babelify",
"browserify-shim"
]
}
}
This is written in my application.js file along with a lot of other stuff which is generated with browserify:
var _jquery = require('jquery');
var _jquery2 = _interopRequireDefault(_jquery);
require('jquery-ujs');