Thursday, 28 June 2018

Node - Import class from module in another package

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!



from Node - Import class from module in another package

No comments:

Post a Comment