Home > Enterprise >  "Cannot use import statement outside a module" without "type: module"
"Cannot use import statement outside a module" without "type: module"

Time:10-18

I've NodeJS app whit many file like this:

import '@babel/polyfill'
import app from './app'

./app is a js file: app.js and @babel/polyfill is a npm package

When I try to start my app with npm run dev i got this error:

SyntaxError: Cannot use import statement outside a module

I've seen that you can use "type": "module" on package.json to solve the problem

But this causes another problem:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module './app' imported from /boot.js

I have many file that import modules and other files like that so i can't change them all

How do I keep the two type fo import?

CodePudding user response:

Apart from using "type": "module" try adding the file extension when importing

import app from './app.js'

and so on.

CodePudding user response:

If you only import babel like that you can setup babel for global import

This is .babelrc file on root folder:

{
    "presets": [
      "@babel/preset-env"
    ]
}
  • Related