【发布时间】:2019-05-19 12:15:30
【问题描述】:
尝试将 moment.js 作为 es6 模块导入。正在使用最新的 Chrome 版本。
根据here的讨论,我尝试了src路径(es6)
import * as moment from './node_modules/moment/src/moment'
虽然跳过 .js 似乎对该线程上的每个人都可以正常工作,但我无法让它工作。但这行得通
import * as moment from './node_modules/moment/src/moment.js'
然而,请求最终失败,导入尝试在没有 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)
我所有的代码都是一个带有这个脚本标签的空白 index.html
<script type="module" src='./main.js'></script>
还有一个带有这个的 main.js 文件
import * as _ from './node_modules/underscore/underscore.js';
import * as moment from './node_modules/moment/src/moment.js';
Underscore.js 导入并且工作正常。问题只是瞬间。 我究竟做错了什么?还有为什么我不能在不指定 .js 扩展名的情况下加载其中任何一个,而显然 ithub 线程上的那些已经能够做到这一点
import * as moment from 'moment'
【问题讨论】:
标签: javascript npm ecmascript-6 momentjs