admin管理员组

文章数量:1401640

I installed jQuery and it is not my question:

npm install jquery

I try to include jQuery library into my page like this:

var jQuery = require('jquery'); or
import jQuery from "jquery";

and get in Chrome console:

Uncaught ReferenceError: $ is not defined

How can I overe it?

I installed jQuery and it is not my question:

npm install jquery

I try to include jQuery library into my page like this:

var jQuery = require('jquery'); or
import jQuery from "jquery";

and get in Chrome console:

Uncaught ReferenceError: $ is not defined

How can I overe it?

Share Improve this question edited Jul 27, 2017 at 12:03 asked Jul 27, 2017 at 7:18 user5965280user5965280 2
  • 1 Possible duplicate of Can I use jQuery with Node.js? – eisbehr Commented Jul 27, 2017 at 7:20
  • Did you try import {jQuery, $} from "jquery";? – 31piy Commented Jul 27, 2017 at 7:24
Add a ment  | 

2 Answers 2

Reset to default 6

You have to use:

var $ = require('jquery');

or

import $ from 'jquery';

In my cases the following works well:

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

or if you prefer "window" or it is obviously present, then:

typeof window !== "undefined" ? window : this;
window.jQuery   = require('jquery');
window.$        = window.jQuery;

本文标签: javascriptNodejs Uncaught ReferenceErroris not defined after var jQueryrequire(39jquery39)Stack Overflow