0

I am using moleculer microservices framework for Node.js. Now, I need to use ES6 import instead of require.

For Example,

const { abc } = require("../../index.js"); **// This is working well.** <br>
import { abc } from "../../index.js";      **// This isn't working and throw the error.**

I am getting error like Unexpected token import. I've installed Babel(JavaScript compiler) as well.

Do you have any idea about this?

Thanks,

Nimesh
  • 2,684
  • 1
  • 24
  • 30
  • 1
    Possible duplicate of [ES6 import statements not working in Node v8.4.0](https://stackoverflow.com/questions/46176973/es6-import-statements-not-working-in-node-v8-4-0) – Arif Khan Sep 21 '18 at 11:12
  • make sure you changed `modules.exports` to `export` in index.js – Aagam Jain Sep 21 '18 at 11:13
  • You have babel installed. How are you *using* babel? – Bergi Sep 21 '18 at 12:07
  • I've just installed babel's require packages (e.g npm install --save-dev @babel/preset-typescript). I don't know how to use babel. Can you please guide me if I am wrong somewhere ? Do I need to add any configurations for Babel ? – Nimesh Sep 21 '18 at 12:12
  • Yes, you actually need to apply Babel to your code and generate the transpiled code, before running *that*. Please read the documentation of Babel. – Bergi Sep 21 '18 at 12:46

0 Answers0