0

I have the following structure.

node_modules
src
 - app.js
 - newrelic.js
package.json
package-lock.json

In newrelic.js its a simple class

const axios = require('axios')

export default class Newrelic {
  static getName() {
   return 'Hello;
  }
}

in app.js

import Newrelic from "./newrelic";
console.log(Newrelic.getName())

When I run node src/app.js Sadly I receive

import Newrelic from "./newrelic";
^^^^^^^
SyntaxError: Cannot use import statement outside a module

What do I need to make this work?

meWantToLearn
  • 1,691
  • 2
  • 25
  • 45
  • Does this answer your question? ["Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6](https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import) – derpirscher Oct 24 '22 at 11:43

2 Answers2

0

In package.json add the field "type": "module" and your code will work.

More info here: https://nodejs.org/api/esm.html

0

It is a very frequent question, when you deal with old packages. For example, it works with 'express-handlebars' v.6.x too. Adding to file package.json the field "type": "module" resolves a similar conflict.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '22 at 15:46