export default class and import not working

Asked

Viewed 523 times

0

I am trying to perform the default export of a class in Node.js, but when I try to compile the main.js file a type error is returned:

Syntaxerror: Unexpected Identifier
at new Script (vm.js:84:7)
creatat eScript (vm.js:264:10)
At object.runInThisContext (vm.js:312:10)
At module. _Compile (Internal/modules/cjs/Loader.js:694:28)
At object.Module. _Extensions.. js (Internal/modules/cjs/Loader.js:745:10)
At module.load (Internal/modules/cjs/Loader.js:626:32)
at tryModuleLoad (Internal/modules/cjs/Loader.js:566:12)
At function.Module. _load (Internal/modules/cjs/Loader.js:558:3)
At function.Module.runMain (Internal/modules/cjs/Loader.js:797:12)
at executeUserCode (Internal/bootstrap/Node.js:526:15)

Can anyone tell me why this happens?

//book.js
export default class Book {
  constructor(title, author) {
    this._title = title;
    this._author = author;
  }
  get title() {
    return this._title;
  }
  set title(title) {
    this._title = title;
  }
  get author() {
    return this._author;
  }
  set author(author) {
    this._author = author;
  }
  printDescription() {
    console.log(`The book ${this._title} was written by ${this._author} `);
  }
}

//main.js
import Book from "./book.js";

const Book01 = new Book("O nome do vento", "Patrick Rothffus");

Book01.printDescription();

  • you have tried to export the class using the module.Exports and then using require to import it?

1 answer

0


In the case of an experimental use there are two ways to do import and export.

The first way is to create a file package.json in the current folder, and then just put into this same file {"type":"module"}. To run the file .js just put:

$ node --experimental-modules index.js 

in place of index.js put the name of the file you want to run.


The second way is in the files you will run to exchange the import code from

import Book from "./book.js";

for

import Book from "./book.mjs";

and change extensions of .js for .mjs, and then execute:

$ node --experimental-modules index.mjs

in place of index.mjs put the name of the file you want to run.


In the case of non-experimental use:

//book.js
class Book {
  constructor(title, author) {
    this._title = title;
    this._author = author;
  }
  get title() {
    return this._title;
  }
  set title(title) {
    this._title = title;
  }
  get author() {
    return this._author;
  }
  set author(author) {
    this._author = author;
  }
  printDescription() {
    console.log(`The book ${this._title} was written by ${this._author} `);
  }

module.exports = Book;

//main.js
const Book = require("./book.js")

const Book01 = new Book("O nome do vento", "Patrick Rothffus");

Book01.printDescription();

Bibliography:

https://thecodebarbarian.com/nodejs-12-imports

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

How to export a Node class?

  • In fact, in version 12 of Node, it is already possible to use ESM Imports, it takes a little work, but it is possible. check out at https://thecodebarbarian.com/nodejs-12-imports

  • Thank you for the comment.

  • Now yes, yours was very good. Thanks, I removed what I had suggested using mjs, because they were the same.

  • Ah, yes. I see. Thank you for the clarification, that was it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.