Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i fix --> File is a CommonJS module; it may be converted to an ES6 module

i stared to java script coding on vs code editor,when i tried to include the external module into my project, the code editor suggest this -->(File is a CommonJS module; it may be converted to an ES6 module.).. is there any way to fix this issue with commend line?my code

like image 776
Nanthagopal nanthu Avatar asked May 01 '20 09:05

Nanthagopal nanthu


People also ask

How do I use the CommonJS module in ES6 module?

An ES6 Module can load other ES6 modules using import. An ES6 Module can load CommonJS using import. A CommonJS module can load other CommonJS modules using require. A CommonJS module previously COULD NOT load an ES6 Module, but today it can load an ES6 module using the import() function.

What does module CommonJS mean?

CommonJS modules are the original way to package JavaScript code for Node. js. Node. js also supports the ECMAScript modules standard used by browsers and other JavaScript runtimes.

What is CommonJS vs ES modules?

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.

What is CommonJS file?

CommonJS is a module formatting system. It is a standard for structuring and organizing JavaScript code. CJS assists in the server-side development of apps and it's format has heavily influenced NodeJS's module management.


1 Answers

Right now the only stable supported module system inside nodejs is commonjs, which is the module system based on the require function and the module global object.

The support to the es6 module system is considered an unstable feature of node, which basically means that the community could decide to change it by introducing breaking changes without respecting the semantic versioning.

So, you basically have 3 choices when it comes to chosing a module system for nodejs:

  • stick with commonjs modules and wait for the es6 modules support to become a stable feature
  • use the currently supported unstable api for es6 modules
  • use es6 modules in your code and, then, use babel to transpile back to commonjs modules so that node can run the transpiled code by using stable apis only

In my opinion the simplest and most effective choice is sticking to commonjs and wait for the es6 module support to become a stable api. At that point, you can safely switch to es6 modules. I would avoid to add complexity by setting up babel and a build process to transpile es6 modules to commonjs modules.

This means that, right now, you can safely ignore that warning in VS code. VS code is hinting you to switch to es6 modules because they have been designed to be the universal module system for javascript, but the adoption process takes time to complete as you can imagine.

There is actually a fourth alternative, but it requires you to adopt typescript (which is, by the way, a good choice for medium to large size projects): write your source code with typescript and let the typescript compiler to transpile it back to node-compatible javascript code.

Typescript uses the es6 module system out of the box, so by adopting typescript you will work with the es6 module system right from the start.

like image 52
Enrico Massone Avatar answered Sep 24 '22 22:09

Enrico Massone