最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nodejs Uncaught ReferenceError: $ is not defined after var jQuery = require('jquery'); - Stack Over

programmeradmin6浏览0评论

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;
发布评论

评论列表(0)

  1. 暂无评论