[Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension

In the process of learning ES6, an error is reported when and running JavaScript files, which is reproduced in detail

1. Create a new file, add test es, and use idea to open the folder
2. Add package.json file: NPM init – y
3. Create a new test.js file in the folder test-es, the contents of which are as follows

let a = 1111
let b = 2222
var c = function(){
	console.log(3333)
}
function ddd(){
	console.log(4444)
}
function eeeeee5(){
	console.log(555)
}
export {
	a,
	b,
 	c,
    ddd,
	eeeeee5 as eee, //The way to rename: name eeeeeeee as eee, you can output the same variable or method with different names
	eeeeee5 as e5 // rename the way: name eeeeee as eee, you can output the same variable or method with a different name
}

4. Right click and click Run ‘test.JS’, the error is reported as follows:

Solution:

According to the error message, in package JSON add “type”: “module”, and then run test.JS file, no error will be reported

Read More: