简体   繁体   English

尝试使用babel-cli将es6代码编译为es5时出错

[英]Error when trying to compile es6 code to es5 using babel-cli

I'm getting an error saying " unexpected token export " when trying to compile some es6 code using " babel-cli " with the presets " es2015 " and " stage-2 ". 当尝试使用预设为“ es2015 ”和“ stage-2 ”的“ babel-cli ”编译某些es6代码时,出现错误消息“ 意外的令牌导出 ”。 I'm trying to export a function from the " test.js " file and import it in the " index.js " file but for some reason it doesn't accept " export " in " test.js ". 我正在尝试从“ test.js ”文件中导出一个函数并将其导入“ index.js ”文件中,但是由于某种原因,它在“ test.js ”中不接受“ export ”。

Each module is installed locally, so I run it from the package.json "scripts" using "build: babel server/index.js -o server/index.babel.js". 每个模块都在本地安装,因此我可以使用“ build:babel server / index.js -o server / index.babel.js”从package.json“ scripts”中运行它。

My " .babelrc " file consists of: 我的“ .babelrc ”文件包括:

 {
  "presets": ["es2015", "stage-2"]
 }

test.js: test.js:

const test = (msg) => {
    console.log(msg)
};

export default test;

index.js: index.js:

index.js: index.js:

import test from './test'

test("Hello")

Any help is much appreciated. 任何帮助深表感谢。

Your code looks fine, and the fact that you're getting that error means that it is reading the files properly. 您的代码看起来不错,并且您收到该错误的事实意味着它正在正确读取文件。 Make sure that you downloaded stage-2 correctly, but I think your error is in that export default is still in stage-1 . 确保您正确下载了stage-2 ,但是我认为您的错误是export default值仍处于stage-1

Run npm install --save-dev babel-preset-stage-1 and add stage-1 to your presets. 运行npm install --save-dev babel-preset-stage-1并将stage-1添加到您的预设中。

That should do it. 那应该做。 Here's a link to https://github.com/leebyron/ecmascript-export-default-from and https://github.com/leebyron/ecmascript-export-ns-from 这是https://github.com/leebyron/ecmascript-export-default-fromhttps://github.com/leebyron/ecmascript-export-ns-from的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM