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

时间:2018-12-19 02:44:20

标签: javascript npm ecmascript-6 momentjs

尝试将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'

1 个答案:

答案 0 :(得分:0)

从ES6开始时,我也遇到类似的问题。您可以尝试通过这种方式导入时刻

import moment from "moment";
相关问题