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

标签 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'

然而,请求最终失败了,因为 import 始终试图在没有 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'

最佳答案

我在使用 ES6 时遇到了类似的问题。你可以试试这种方式导入moment

import moment from "moment";

关于javascript - 无法从支持的浏览器导入 es6 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843867/

相关文章:

javascript - 在 Canvas 上绘图表演 buggy

node.js - npm 链接重新创建 .nvm 目录

javascript - 单击 React 元素列表并将其作为值存储在另一个列表中

javascript - ES6 (ECMAScript 2015) 模块 : import index. js

javascript - 这个箭头函数可以缩短吗?这些是一样的吗?

javascript - 从表单条目追加到 JSON 文件

javascript - 服务器端 requirejs 优化 (r.js) 多页站点

javascript - Reacts `propTypes` 和 `defaultProps` 应该和 Flowtype 一起使用,还是 Flowtype 够全面?

android - 错误 ENOENT : no such file or directory, 打开 'app/src/main/AndroidManifest.xml'

node.js - 在Dockerfile中进行npm构建时不会生成'dist'文件夹