6

I'm writing an app using Node.js. Specifically, I'm using Node v10.3.0. This app is located in a directory located at ./my-module-console/index.js. This app have a package.json file located at ./my-module-console/package.json. This app references a class defined in ./my-module/items/. It should be noted that my-module represents its own package. That package is defined in ./my-module/package.json. The code in index.js looks like this:

'use strict';

import { Item } from '../my-module/items/item.js';

async function entry() {
  let item = await Item.FindById(1);
  console.log(item);
}

entry();

When I attempt to run this, I get the following error:

import { Item } from '../my-module/items/item.js';
       ^
SyntaxError: Unexpected token {

What is wrong with my import statement? It looks correct to me. Am I misunderstanding something?

item.js

class Item {
    constructor() {}

    async static FindById(id) {
      console.log('finding item by id: ' + id);
    }
};

module.exports = Item;

Thanks!

Some User
  • 3,779
  • 8
  • 39
  • 75

5 Answers5

5

As mentioned by @jsur ES Modules are still experimental. But if you want to use them you have to add --experimental-modules. If you still want to use ES Modules then you have to rename .js to .mjs and also the item.js, it is commonJS style now, has to be changed to ES Modules + small other fix. And also you don't really have to use 'use strict', it is strict by default. So finally it should look like this:

index.mjs

import { Item } from '../my-module/items/item';

async function entry() {
  let item = await Item.FindById(1);
  console.log(item);
}

entry();

item.mjs

export class Item {
    constructor() {}

    static async FindById(id) {
      console.log('finding item by id: ' + id);
    }
}

So now just do node --experimental-modules index.mjs and you're good to go.

evgeny.myasishchev
  • 3,039
  • 1
  • 17
  • 13
0

import { Item } from '../my-module/items/item.js'; is the ES6 syntax for importing exports from Javascript modules. Node does not support this currently without additional flags, so you'll have to use require instead:

const item = require('../my-module/items/item');

This way you'll be requiring the class Item being exported in item.js.

Also take note that you have to create an instance of the class you're exporting to use its functions in index.js.

jsur
  • 263
  • 2
  • 9
0

First:

class Item {
    constructor() {}

    async static FindById(id) {
      console.log('finding item by id: ' + id);
    }
};

module.exports.Item = Item; // change this line

Seccond:

'use strict';

// import { Item } from '../my-module/items/item.js'; // not sure if it work, it work in react, but in regular node.js can be wrong (look doc ES6)
const Item = require('../my-module/items/item.js');
var someitem = new Item();
async function entry() {
  let item = await someitem.FindById(1);
  console.log(item);
}
entry();
Denis Rohlinsky
  • 172
  • 2
  • 12
0

import is experimental currently. As per docs

The --experimental-modules flag can be used to enable features for loading ESM modules.

Once this has been set, files ending with .mjs will be able to be loaded as ES Modules.

node --experimental-modules my-app.mjs

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

Waqas Noor
  • 881
  • 6
  • 12
0

Import is not yet supported unless you use babel compiler to compile your ES6 code and then run it.

Using experimental-modules is for experiments i.e development environment. And renaming to .mjs isn't a good idea as eventually it's gonna change.

If you want to use babel js: Write code in ES6 and compile and run it.

Getting started with Babel

Optionally,

Here's how you can quickly solve problem and move ahead:

const item = require('../my-module/items/item');
  • Read more about Babel here.
  • About Import here.
Harshal Y.
  • 4,143
  • 1
  • 15
  • 39