gpt4 book ai didi

ecmascript-6 - REPL 支持 babel-node es6 "Modules aren' t"

转载 作者:行者123 更新时间:2023-12-04 17:17:07 26 4
gpt4 key购买 nike

babel-preset-es2015已安装,并且可以使用 es6 功能,如下面 let a = 2; .
但无法使用 es6 模块功能 import fs from 'fs'如下:

$ babel-node --presets es2015
> let a = 2;
'use strict'
> a
2
> import fs from 'fs';
SyntaxError: repl: Modules aren't supported in the REPL
import fs from 'fs';
at File.buildCodeFrameError (/usr/lib/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/index.js:407:15)
at NodePath.buildCodeFrameError (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/index.js:149:26)
at PluginPass.ModuleDeclaration (/usr/lib/node_modules/babel-cli/lib/_babel-node.js:78:20)
at newFn (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/visitors.js:262:19)
at NodePath._call (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:63:18)
at NodePath.call (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:47:17)
at NodePath.visit (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/path/context.js:93:12)
at TraversalContext.visitQueue (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:152:16)
at TraversalContext.visitMultiple (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:110:17)
at TraversalContext.visit (/usr/lib/node_modules/babel-cli/node_modules/babel-traverse/lib/context.js:182:19)

那么有什么问题呢?
谢谢!

最佳答案

错误消息正是它所说的。您不能在 REPL 中使用 ES6 模块语法,它不受支持。您可以创建一个小型适配器,以 ES6 形式导入并以 CommonJS 形式导出:

# es6-to-common.js
import MyThing from './somewhere';
module.exports = MyThing;

现在在你平常的 babel-node里面迅速的:
> MyThing = require('./es6-to-common')

关于ecmascript-6 - REPL 支持 babel-node es6 "Modules aren' t",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36197012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com