如果你想在 NodeJS 中使用你的 HTML 文件中加载的 jQuery,你需要先将它与 $ 关联起来:>
转到要使用它的脚本,然后写入:
window.$ = window.jQuery;
如果这不起作用,请通过在脚本文件夹中打开终端然后键入:
来安装 jQuery npm 包
npm i jquery
然后写
window.$ = window.jQuery = require("jquery");
在你的脚本中.
I have this error message ReferenceError: $ is not defined when using vscode built-in debugger node.js Here is the html
<!doctype html>
<html lang="en">
<head>
<title>14. Getting Started with jQuery</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript" src="js/app.js"></script>
</body>
</html>
Here is the app.js
$(function() {
// start up code goes here
alert("this works!");
});
I put a breakpoint at the alert line and run the debug (node.js) in vscode. It stopped at $(function() { - the first line of app.js with error message of ReferenceError: $ is not defined. Seems like jQuery is not loaded.