Monday, 15 May 2017

NodeJs TypeError: Cannot read property 'jquery' of undefined

I try to add bootstrap v4 with jquery to my project. My index.js file show like below:

'use strict';

const handler = require('feathers-errors/handler');
const notFound = require('./not-found-handler');
const logger = require('./logger');

global.jQuery = require('jquery');
require('bootstrap');

module.exports = function() {
  // Add your custom middleware here. Remember, that
  // just like Express the order matters, so error
  // handling middleware should go last.
  const app = this;

  app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
  app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
  app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap

  app.use(notFound());
  app.use(logger(app));
  app.use(handler());
};

Unfortunately after running I have an error:

/home/seadog/workspace/aloes/node_modules/bootstrap/dist/js/bootstrap.js:12
  var version = $.fn.jquery.split(' ')[0].split('.')
                    ^

TypeError: Cannot read property 'jquery' of undefined
    at /home/seadog/workspace/aloes/node_modules/bootstrap/dist/js/bootstrap.js:12:21
    at Object.<anonymous> (/home/seadog/workspace/aloes/node_modules/bootstrap/dist/js/bootstrap.js:16:2)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/seadog/workspace/aloes/src/middleware/index.js:8:1)

I installed bootstrap: npm install bootstrap@4.0.0-alpha.6 --save

and jQuery too: npm install jquery --save

What am I doing wrong?



via SeaDog

No comments:

Post a Comment