简体   繁体   English

Babel ES6:导入需要在ES6中编译的节点模块

[英]Babel ES6: Import node modules that need to be transpiled in ES6 as well

I am importing a node module correctly but run into an issue where the node module I am importing is written in es6 and babel is unable to transpile it. 我正在正确导入节点模块但遇到了一个问题,我导入的节点模块是用es6编写的,而babel无法传输它。

In base class header: 在基类标题中:

import foo from 'bar/lib/foo';

in foo.js in node modules: 在节点模块中的foo.js中:

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

}

Error Message: 错误信息:

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

SyntaxError: Unexpected token import

As you can see it is able to find the file foo.js but it is not running the node module in es6. 如您所见,它能够找到文件foo.js,但它没有在es6中运行节点模块。 How can I have Babel transpile both the base code as well as the node module that it is trying to import? 如何让Babel转换基本代码以及它尝试导入的节点模块?

Do you have a file .babelrc with this content? 你有这个内容的文件.babelrc吗?

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

You can check an example here: https://github.com/Talento90/ima-up 您可以在此处查看示例: https//github.com/Talento90/ima-up

The node module should be compiled separately from your source code. 节点模块应与源代码分开编译。 If you are using an external lib, they should have a directory with the transpiled code. 如果您使用的是外部库,则应该有一个包含已转换代码的目录。 If you are writing your own, you need to use npm link and compile it separately. 如果您自己编写,则需要使用npm link并单独编译。

If the node module already has a transpiled directory (like dist ), you could try importing the transpiled code into the node module: 如果节点模块已经有一个转换目录(如dist ),您可以尝试将转换后的代码导入节点模块:

import foo from 'bar/dist/foo';

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

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