Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import class using ES6 syntax? (without Babel)

I have file with a ES6 class with default export:

accessmanager.js

   export default class AccessManagerClient {
      constructor(){
        ...
      }
    }

And I want to import it like this:

anotherFile.js

import AccessManagerClient from '../someFolder/accessmanager';

When I run my spec I get an error:

import AccessManagerClient from '../someFolder/accessmanager';
^^^^^^

SyntaxError: Unexpected token import

I have such npm and node versions:

$ npm -v
5.3.0

$ node -v
v8.0.0
like image 947
Oleksii Avatar asked Jan 30 '23 17:01

Oleksii


2 Answers

You can't without Babel or some other packager/bundler/transpiler, because Node doesn't have native support for import/export yet. Either use Node's own modules, or a packager/bundler/transpiler.

like image 159
T.J. Crowder Avatar answered Feb 01 '23 07:02

T.J. Crowder


I know that this is an old question. But in case others still look for alternative, now with the new Node version, we can use without Babel:

https://nodejs.org/api/esm.html

So what you need to do is upgrading your Node version. FYI: This Node version v10.9.0 is still experimental.

like image 28
Đỗ Ngọc Hoan Avatar answered Feb 01 '23 06:02

Đỗ Ngọc Hoan