A
A
Andrew2019-10-18 11:45:42
Node.js
Andrew, 2019-10-18 11:45:42

SyntaxError: Cannot use import statement outside a module?

NodeJs version 12.
Swears on any import. require works fine, but it became interesting why the import does not work and what is this strange error (I did not find an answer in Google). You can't use imports outside of a module, but how can you define that module?
import express from 'express'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir, 2019-11-17
@HistoryART

Decided using this

let app = 123;

module.exports = { app }

And connect
const { app } = require('./app')

P
Penoplaster83, 2020-07-26
@Penoplaster83

You need to add this
"type": "module" to package.json

A
adenisov, 2019-11-17
@adenisov

Good afternoon.
Apparently, you do not have Babel configured. Those. either use the old notation (require) or install babel

npm install --save-dev @babel/core @babel/cli @babel/node @babel/preset-env

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question