Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must use import to load ES Module NODEJS Error in Babel Module

I am new to this babel and with some experience in nodejs. My package.json files has a babel

"@babel/preset-react": "^7.10.4", "@nuxtjs/eslint-config": "^0.0.1", "babel-cli": "^6.26.0",

These dependencies related to babel. Got this project form some developer who is unable to work. After installing dependencies and other things. When I try to run the project via nuxtjs give me following error:

.........................................................................................................................................................................................

Must use import to load ES Module: /home/jaskaran/Downloads/electrictourcompany.com-master/node_modules/@babel/runtime/helpers/esm/objectSpread2.js require() of ES modules is not supported. require() of /home/jaskaran/Downloads/electrictourcompany.com-master/node_modules/@babel/runtime/helpers/esm/objectSpread2.js from /home/jaskaran/Downloads/electrictourcompany.com-master/node_modules/vue-server-renderer/build.dev.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename objectSpread2.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/jaskaran/Downloads/electrictourcompany.com-master/node_modules/@babel/runtime/helpers/esm/package.json.

............................................................................................................................................................................

I have already tried renaming and removing the type module from the node_module folder which is usually recommend. Now I am lost on moving forward

like image 388
Jaskaran Singh Avatar asked Nov 06 '22 06:11

Jaskaran Singh


People also ask

How do I resolve a Node module error?

To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.

Should I use import or require in NodeJS?

NOTE: You must note that you can't use require and import at the same time in your node program and it is more preferred to use require instead of import as you are required to use the experimental module flag feature to run import program.

How do you solve require () of ES modules is not supported?

You can solve the "[ERR_REQUIRE_ESM]: require() not supported" by doing one of two things: Use ESM - use import foo from 'foo' , instead of const foo = require('foo') and add the following line to your package. json file: "type": "module" . Downgrade to the last version of the package that is built with CommonJS .


1 Answers

I understand it's been a year but did you try upgrading or downgrading the version of @babel/runtime?

I'm using version 7.14.6 and it doesn't have the "type: module" and it works fineenter image description here

this is a known issue with node v12.11.0 which was resolved in v12.11.1

If you're facing this issue because of sub-dependencies you can add the working version to resolutions in your package.json.

like image 158
deechris27 Avatar answered Nov 12 '22 18:11

deechris27