ES6 导入 npm 模块
ES6 Importing npm module
我刚开始进行 Web 开发,但无法将我使用 npm i MODULE -S
安装的模块导入 script
中的 .html
。我的文件结构类似于:
设置:
project
|_ node_modules
|_ public
| |_ css
| |_ js
| | |_ libs
| | |_ index.mjs
| |_ index.html
|_ server
|_ server.mjs
index.html
文件非常简单,类似于:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/styles.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSON and AJAX</title>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> -->
</head>
<body>
<header>
<h1>JSON and AJAX</h1>
<button id="btn">Fetch Info for 3 New Objects</button>
</header>
<div id="animal-info"></div>
<script type="module" src="js/index.mjs"></script>
<div id="msgid"></div>
</body>
</html>
和 index.mjs
文件:
import $ from 'jquery';
为了完整起见,server.mjs
文件
// const path = require('path');
import path from 'path';
import express from 'express';
const app = express();
const __dirname = path.resolve();
const publicPath = path.join(__dirname, '/public');
app.use(express.static(publicPath));
const port = process.env.PORT || 8080;
app.set('port', port);
app.listen(app.get('port'), () => {
console.log(`listening on port ${port}`);
});
问题
当我 运行 node --experimental-modules server/server.mjs
我的页面加载并且我能够在 localhost:8080
中访问它时,但是当我在 chrome 中打开开发者工具时我' m 出现以下错误:
Uncaught TypeError: Failed to resolve module specifier "jquery". Relative references must start with either "/", "./", or "../".
已尝试解决
1) 我已将 index.mjs
文件中的导入语句更改为:
import $ from '.jquery';
import $ from './jquery';
'import $ from '../../node_modules/jquery';
输出以下消息:
GET http://localhost:49160/js/jquery net::ERR_ABORTED 404 (Not Found)
GET http://localhost:49160/js/jquery net::ERR_ABORTED 404 (Not Found)
GET http://localhost:49160/node_modules/jquery net::ERR_ABORTED 404 (Not Found)
2) 我试图将 jquery
文件夹从 node_modules
目录复制到 libs
目录并重新 运行 以便 index.js
只有以下代码:
import $ from './libs/jquery';
但我收到以下错误:
GET http://localhost:49160/js/libs/jquery/ net::ERR_ABORTED 404 (Not Found)
额外
当我按照 Mozilla developer 页面上的文档开发自己的模块时,一切正常,但是当我尝试使用第三方模块时,出现了一系列错误。
正确的导入是
import * as $ from 'jquery';
TLDR;
import $ from '/js/libs/jquery/dist/jquery.js'
或
import $ from './libs/jquery/dist/jquery.js'
你的两次尝试有两个不同的问题。
import $ from 'jquery';
这种按名称导入的方式将不起作用,因为您的浏览器不知道在哪里搜索 jquery。根据浏览器的不同,如果您在服务器的根目录 (/public) 中有一个 jquery.js
文件,它 可能 可以工作,但不能保证。
import $ from '.jquery';
import $ from './jquery';
'import $ from '../../node_modules/jquery';
不幸的是,这些都是错误的路径。 ES6 模块从文件加载,而不是从目录加载,package.json 被忽略。所以所有这些最后都需要 dist/jquery.js
才有机会。此外,根据您的目录结构,none 是正确的(假设 jquery 目录在 /public/js/libs 中)。第二个是收尾,但开头缺少 ./libs
。
CDN
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
NPM 类型安装
npm install @types/jquery --save-dev
TypeScript 参考
/// <reference types="jquery" />
关键是要意识到,jQuery 可以通过全局范围访问
https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html#global-libraries
并且可以通过 reference
关键字而不是 import
来引用
https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html#dependencies-on-global-libraries
我刚开始进行 Web 开发,但无法将我使用 npm i MODULE -S
安装的模块导入 script
中的 .html
。我的文件结构类似于:
设置:
project
|_ node_modules
|_ public
| |_ css
| |_ js
| | |_ libs
| | |_ index.mjs
| |_ index.html
|_ server
|_ server.mjs
index.html
文件非常简单,类似于:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/styles.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JSON and AJAX</title>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> -->
</head>
<body>
<header>
<h1>JSON and AJAX</h1>
<button id="btn">Fetch Info for 3 New Objects</button>
</header>
<div id="animal-info"></div>
<script type="module" src="js/index.mjs"></script>
<div id="msgid"></div>
</body>
</html>
和 index.mjs
文件:
import $ from 'jquery';
为了完整起见,server.mjs
文件
// const path = require('path');
import path from 'path';
import express from 'express';
const app = express();
const __dirname = path.resolve();
const publicPath = path.join(__dirname, '/public');
app.use(express.static(publicPath));
const port = process.env.PORT || 8080;
app.set('port', port);
app.listen(app.get('port'), () => {
console.log(`listening on port ${port}`);
});
问题
当我 运行 node --experimental-modules server/server.mjs
我的页面加载并且我能够在 localhost:8080
中访问它时,但是当我在 chrome 中打开开发者工具时我' m 出现以下错误:
Uncaught TypeError: Failed to resolve module specifier "jquery". Relative references must start with either "/", "./", or "../".
已尝试解决
1) 我已将 index.mjs
文件中的导入语句更改为:
import $ from '.jquery';
import $ from './jquery';
'import $ from '../../node_modules/jquery';
输出以下消息:
GET http://localhost:49160/js/jquery net::ERR_ABORTED 404 (Not Found)
GET http://localhost:49160/js/jquery net::ERR_ABORTED 404 (Not Found)
GET http://localhost:49160/node_modules/jquery net::ERR_ABORTED 404 (Not Found)
2) 我试图将 jquery
文件夹从 node_modules
目录复制到 libs
目录并重新 运行 以便 index.js
只有以下代码:
import $ from './libs/jquery';
但我收到以下错误:
GET http://localhost:49160/js/libs/jquery/ net::ERR_ABORTED 404 (Not Found)
额外
当我按照 Mozilla developer 页面上的文档开发自己的模块时,一切正常,但是当我尝试使用第三方模块时,出现了一系列错误。
正确的导入是
import * as $ from 'jquery';
TLDR;
import $ from '/js/libs/jquery/dist/jquery.js'
或
import $ from './libs/jquery/dist/jquery.js'
你的两次尝试有两个不同的问题。
import $ from 'jquery';
这种按名称导入的方式将不起作用,因为您的浏览器不知道在哪里搜索 jquery。根据浏览器的不同,如果您在服务器的根目录 (/public) 中有一个 jquery.js
文件,它 可能 可以工作,但不能保证。
import $ from '.jquery';
import $ from './jquery';
'import $ from '../../node_modules/jquery';
不幸的是,这些都是错误的路径。 ES6 模块从文件加载,而不是从目录加载,package.json 被忽略。所以所有这些最后都需要 dist/jquery.js
才有机会。此外,根据您的目录结构,none 是正确的(假设 jquery 目录在 /public/js/libs 中)。第二个是收尾,但开头缺少 ./libs
。
CDN
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
NPM 类型安装
npm install @types/jquery --save-dev
TypeScript 参考
/// <reference types="jquery" />
关键是要意识到,jQuery 可以通过全局范围访问 https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html#global-libraries
并且可以通过 reference
关键字而不是 import
来引用
https://www.typescriptlang.org/docs/handbook/declaration-files/library-structures.html#dependencies-on-global-libraries