emarel
emarel

Reputation: 401

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.

In base class header:

import foo from 'bar/lib/foo';

in foo.js in node modules:

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. How can I have Babel transpile both the base code as well as the node module that it is trying to import?

Upvotes: 3

Views: 3184

Answers (2)

Bobby Matson
Bobby Matson

Reputation: 1488

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.

If the node module already has a transpiled directory (like dist), you could try importing the transpiled code into the node module:

import foo from 'bar/dist/foo';

Upvotes: 1

Marco Talento
Marco Talento

Reputation: 2395

Do you have a file .babelrc with this content?

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

You can check an example here: https://github.com/Talento90/ima-up

Upvotes: 1

Related Questions