【问题标题】:Babel ES6: Import node modules that need to be transpiled in ES6 as wellBabel ES6:导入需要在 ES6 中转译的节点模块
【发布时间】:2016-06-07 04:24:30
【问题描述】:

我正在正确导入节点模块,但遇到了一个问题,即我正在导入的节点模块是用 es6 编写的,babel 无法转译它。

在基类头中:

import foo from 'bar/lib/foo';

在节点模块中的 foo.js 中:

import Debug from 'debug';
export default class foo from bar {
    ...
    ...
    ...

}

错误信息:

import Debug from 'debug';
^^^^^^

SyntaxError: Unexpected token import

如您所见,它能够找到文件 foo.js,但它没有在 es6 中运行节点模块。如何让 Babel 转译基本代码以及它尝试导入的节点模块?

【问题讨论】:

  • 你在说任何特定的模块吗?

标签: node.js import ecmascript-6 babeljs


【解决方案1】:

节点模块应该与您的源代码分开编译。如果您使用的是外部库,它们应该有一个包含已转译代码的目录。如果是自己写,需要使用npm link,单独编译。

如果节点模块已经有一个转译目录(如dist),您可以尝试将转译后的代码导入节点模块:

import foo from 'bar/dist/foo';

【讨论】:

    【解决方案2】:

    你有包含此内容的文件 .babelrc 吗?

    {
       "presets": ["es2015"],
       "plugins": []
    }
    

    您可以在此处查看示例:https://github.com/Talento90/ima-up

    【讨论】:

    • 我在我的基本文件夹的根目录中做,但不在节​​点模块'bar'文件夹中。它也应该在那里吗?
    • 你是如何编译 já 文件的?通过 Gulp?
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2017-01-06
    • 2018-07-10
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多