我使用 npm -install jquery 安装了 jQuery,它在我的项目中创建了一个包含 jquery 的 node_modules 文件夹。但是当我尝试使用 ES6 import 导入它时,它给了我一个错误。
我不想使用 Webpack 或 require() 并且必须编译它......其他任何东西都只是普通的 ES6。
我总是收到此错误
Uncaught SyntaxError: The requested module './node_modules/jquery/dist/jquery.min.js' does not provide an export named '$'
或
Uncaught SyntaxError: The requested module './node_modules/jquery/src/jquery.js' does not provide an export named '$'
项目结构
.
├── index.html
├── app.js
├── node_modules/
│ ├── jquery/
│ │ ├── dist/
│ │ │ ├── jquery.js
│ │ │ ├── jquery.min.js
│ │ ├── src/
│ │ │ ├── jquery.js
└── package.json
app.js
import { $ } from './node_modules/jquery/dist/jquery.min.js'; // <-- does not work
import { $ } from './node_modules/jquery/src/jquery.js'; // <-- does not work
window.$ = $;
$('body').css('background-color', 'red')
最佳答案
尝试npm install jquery
,然后从“jquery”导入$
。
关于javascript - 如何仅使用 ES6 从 npm 包导入 jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593243/