无法从支持的浏览器导入 es6 模块

Stu*_*orn 5 javascript npm momentjs ecmascript-6

尝试将 moment.js 作为 es6 模块导入。正在使用最新的 Chrome 版本。
根据此处的讨论,我尝试了 src 路径(es6)

import * as moment from './node_modules/moment/src/moment'
Run Code Online (Sandbox Code Playgroud)

虽然跳过 .js 似乎对该线程上的每个人都工作得很好,但我无法让它工作。但这有效

import * as moment from './node_modules/moment/src/moment.js'
Run Code Online (Sandbox Code Playgroud)

然而,请求最终失败,导入尝试在没有 js 扩展的情况下加载其依赖项

 GET http://127.0.0.1:8083/node_modules/moment/src/lib/utils/hooks net::ERR_ABORTED 404 (Not Found) moment.js:22
 GET http://127.0.0.1:8083/node_modules/moment/src/lib/moment/moment net::ERR_ABORTED 404 (Not Found) moment.js:26 
 GET http://127.0.0.1:8083/node_modules/moment/src/lib/moment/calendar net::ERR_ABORTED 404 (Not Found) moment.js:39 
 GET http://127.0.0.1:8083/node_modules/moment/src/lib/locale/locale net::ERR_ABORTED 404 (Not Found) moment.js:46 
 GET http://127.0.0.1:8083/node_modules/moment/src/lib/duration/duration net::ERR_ABORTED 404 (Not Found) moment.js:48 
 GET http://127.0.0.1:8083/node_modules/moment/src/lib/units/units net::ERR_ABORTED 404 (Not Found) moment.js:50 
 GET http://127.0.0.1:8083/node_modules/moment/src/lib/utils/is-date net::ERR_ABORTED 404 (Not Found) 
Run Code Online (Sandbox Code Playgroud)

我所拥有的代码是带有此脚本标记的空白index.html

<script type="module" src='./main.js'></script>
Run Code Online (Sandbox Code Playgroud)

和一个 main.js 文件

import * as _ from './node_modules/underscore/underscore.js';
import * as moment from './node_modules/moment/src/moment.js';
Run Code Online (Sandbox Code Playgroud)

Underscore.js 导入并运行良好。问题仅在于时刻。我究竟做错了什么?另外,为什么我无法在不指定 .js 扩展名的情况下加载其中任何一个,而显然那些在 ithub 线程上的人已经能够做到这一点

import * as moment from 'moment'
Run Code Online (Sandbox Code Playgroud)

Tun*_*ong 0

当我开始使用 ES6 时,我也遇到了类似的问题。您可以尝试这种方式导入时刻

import moment from "moment";
Run Code Online (Sandbox Code Playgroud)

  • 未捕获的类型错误:无法解析模块说明符“时刻”。相对引用必须以“/”、“./”或“../”开头。 (2认同)