Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'type: module' in package.json file?

I upgraded the node and built the existing file.

But it didn't build, and there was an error.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:                          │    │   ~~/nuxt.config.js                                      │    │   require() of ES modules is not supported.                                            │    │   require() of ~~/nuxt.config.js from                    │    │   ~~/config.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 nuxt.config.js to end in .cjs, change the requiring code to use       │    │   import(), or remove "type": "module" from                                            │    │   ~~/package.json.   

So I removed 'type: module' in package.json file.

Is it okay to remove it?

like image 268
Rebbeca Avatar asked Apr 24 '20 05:04

Rebbeca


People also ask

What does type module mean?

A script tag having type="module" attribute specifies that it to be considered as a Javascript module. It may be importing other Javascript module(s) inside it and becomes a "top-level" module for the imported modules.

What is module field in package json?

The main field makes sure that Node users using require will be served the UMD version. The module field is not an official npm feature but a common convention among bundlers to designate how to import an ESM version of our library.

How do I add a module to a package json?

To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.

What does package json file contains?

Your package. json holds important information about the project. It contains human-readable metadata about the project (like the project name and description) as well as functional metadata like the package version number and a list of dependencies required by the application.


1 Answers

When you have 'type': 'module' in the package.json file, your source code should use import syntax. When you do not have, you should use require syntax.

Adding 'type': 'module' to the package.json enables ES 6 modules. For more info, see here.

like image 89
Afshar Mohebi Avatar answered Oct 03 '22 06:10

Afshar Mohebi