javascript - babel 子模块意外 token 导入

标签 javascript node.js babeljs

我找不到我的问题的解决方案,这里是:
我使用 babel 在我的 api 上将我的代码从 es6 转换为 es5。
在我添加一个包含 API 和我的移动应用程序 (React Native) 之间的共享函数的子模块之前,这一直很好用。
现在,当我从这个公共(public)子模块导入一些东西时,出现错误“Unexpected token 'import'”,所以我的子模块没有被转译。
在移动应用程序上注意,我对这个子模块没有问题。

这是我的结构:
|来源
| -- 通用/(子模块)
| -- -- graphql/
| -- -- -- user.js(必填,es6编写)
| -- -- 包.json
| -- 数据/
| -- 助手/
| -- 索引.js

我的 babelrc 看起来像:

{
  "plugins": [
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-arrow-functions",
    "@babel/plugin-transform-regenerator",
    "@babel/plugin-transform-runtime",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-class-properties"
  ],
  "presets": ["@babel/preset-env"]
}

注意我的子模块没有任何.babelrc

这里是 babel error

有人遇到过这个问题吗? 谢谢 :)

编辑:如果我删除子模块 package.json,它会工作

编辑 2:将我的 .babelrc 重命名为 babel.config.js 成功了 ✅

Node v8.10.0
巴别塔 7.0.0

最佳答案

这是预期的行为。如果你看https://babeljs.io/docs/en/config-files ,关键部分是

The "filename" being compiled must be inside of "babelrcRoots" packages, or else searching will be skipped entirely.

src/common/graphql/user.jssrc/common 里面包,但你的根包是根,这是一个不同的包。

如果您想使用内部 .babelrc您需要通过创建 babel.config.js 来选择加入根目录下的文件

module.exports = {
  babelrcRoots: [__dirname, __dirname + "/common"],
};

让 Babel 知道这两个包都应该被考虑 .babelrc可用的包。

关于javascript - babel 子模块意外 token 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990965/

相关文章:

javascript - Socket.io ReferenceError 客户端未定义

javascript - 使用jquery在div中添加一个又一个动画

node.js - API.ai/Dialogflow 基本文本请求不起作用

node.js - 错误 TS5014 : Failed to parse file '' : Unexpected token u in JSON at position 0. 终端进程终止,退出代码:1

node.js - 加快我的 cloudant 查询速度

javascript - 找不到入口模块和 webpack 中的错误

javascript - 简单的功能给出意想不到的结果

javascript - 如何向 rel ="value"添加多个标签/字符串

unit-testing - 如何使用 JSDom 对 Jest 中的自定义元素执行单元测试

javascript - 获取预编译源代码的 Karma 代码覆盖率