简体   繁体   中英

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

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.

You can disable them by adding "javascript.suggestionActions.enabled": false to your vscode settings.

These are suggestions/hints. They visually indicate that vscode can perform an action to possibly refactor/improve your code, but they are not treated as errors. You can disable them by adding "javascript.suggestionActions.enabled": false to your user settings

From vscode official repo - https://github.com/microsoft/vscode/issues/47299#issuecomment-379335075

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM