This is a new feature added in Visual Studio Code called "Suggestion Code Actions". "Suggestion Code Actions" are enabled by default in JavaScript and TypeScript.

You can disable them by setting: "typescript.suggestionActions.enabled": false or "javascript.suggestionActions.enabled": false in your user/workspace settings. The documentation can be found here.

(Image provided by Yusuf Yaşar.)

Answer from HSir on Stack Overflow
🌐
GitHub
github.com › microsoft › vscode › issues › 47299
[js] File is a CommonJS module; it may be converted to an ES6 module. · Issue #47299 · microsoft/vscode
April 6, 2018 - [js] File is a CommonJS module; it may be converted to an ES6 module.#47299 · Copy link · Assignees ·
Author   ynwd
Discussions

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 More on stackoverflow.com
🌐 stackoverflow.com
Error "File is a commonJS module; it may be converted to an ES6 module. ts(80001)"
Developing JavaScript in VSCode, I see this error on line const Koa = require("Koa");: File is a CommonJS module; it may be converted to an ES6 module. ts(80001) and after I searched, I... More on stackoverflow.com
🌐 stackoverflow.com
Trouble using require(); Needing to convert JS file to ES module
When I use require() in my JS file, I get the following error: File is a CommonJS module; it may be converted to an ES module. Why is it so? And how can I use require, ie, convert my JS file into a... More on stackoverflow.com
🌐 stackoverflow.com
Conversion to ES Module should not change whole-module import to named imports
Suggestion 🔍 Search Terms convert es module default and named import ✅ Viability Checklist My suggestion meets these guidelines: This wouldn't be a breaking change in existing TypeScript/JavaSc... More on github.com
🌐 github.com
3
February 13, 2022
🌐
LogRocket
blog.logrocket.com › home › commonjs vs. es modules in node.js
CommonJS vs. ES modules in Node.js - LogRocket Blog
June 20, 2024 - However, it’s important to remember that for Node.js to treat a module as an ES module, one of the following must happen: either the module’s file extension must convert from .js (for CommonJS) to .mjs (for ES modules) or we must set a {"type": "module"} field in the nearest package.json file.
🌐
Brainly
brainly.com › computers and technology › high school › a commonjs module may be converted to an es6 module. true or false?
[FREE] A CommonJS module may be converted to an ES6 module. True or False? - brainly.com
June 13, 2023 - If you have a file that is a CommonJS module, you can convert it to an ES6 module by using a transpiler. A transpiler is a tool that converts code from one language to another.
🌐
AppSignal
blog.appsignal.com › 2024 › 12 › 11 › a-deep-dive-into-commonjs-and-es-modules-in-nodejs.html
A Deep Dive Into CommonJS and ES Modules in Node.js | AppSignal Blog
December 11, 2024 - By updating the file extension and module syntax within the file, you can choose which files to convert to ESM while leaving the rest as CommonJS. The .cjs extension explicitly marks a file as a CommonJS module, even if the project has "type": ...
Top answer
1 of 1
22

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.

Top answer
1 of 2
1

"File is a CommonJS module; it may be converted to an ES module"

This is not error, just a warning. You can still use commonjs and require() (provided that the package supports CommonJs). It will still work. Preferrably, you can change your extension name to .cjs to mark your file as a commonjs file and the warning should disappear

The bigger context here is that Typescript/JavaScript is trying to encourage people to use the new ES6 module system, with import and export instead of the old CommonJS with require() and exports.

You can use the new ES6 module import/export syntax, by adding to the package.json this property: "type": "module" and then instead of require() you use import ... from.

Many modern packages have changed to support ES6 modules, so it is recommended that you also use ES6 module syntax. But again, you can still use require() if you prefer if the package you import supports commonjs

Similar question: Error "File is a commonJS module; it may be converted to an ES6 module. ts(80001)"

2 of 2
1

You must notice that the require() is a CommonJS directive that it is requesting a ESModule like it was a CommonJS. EDIT: It will always throw an error message because it's not supported.

Of course, you still must take in consideration this other answer by Bao Huynh Lam in the list here. It's pretty useful information.

You can dynamically import() any ESModule inside your CommonJS file.

Here's an example:

some-file.cjs (which is CommonJS)

new Promise
(
   ( resolve, reject ) =>
   {
      import('./module.mjs').then
      (
         result => resolve(result),
         reason => reject(reason),
      )
   }
)
.then
(
   moduleImported => console.log('Module imported dynamically:', moduleImported),
   moduleRejected => console.log('Failed to retrieve module:', moduleRejected),
);

module.mjs (which is ESModule)

export default { moduleFile: import.meta.url };
Find elsewhere
🌐
Deno
deno.com › blog › convert-cjs-to-esm
How to convert CommonJS to ESM | Deno
October 16, 2024 - With VSCode, you can quickly convert all import and export statements from CommonJS to ESM. Simply hover over the require keyword, hit “quick fix”, and all of those statements in that file will be updated to ESM:
🌐
GitHub
github.com › microsoft › TypeScript › issues › 47869
Conversion to ES Module should not change whole-module import to named imports · Issue #47869 · microsoft/TypeScript
February 13, 2022 - I'm currently using the File is a CommonJS module; it may be converted to an ES module. diagnostic to refactor CJS modules to ESM.
Author   Josh-Cena
🌐
Reddit
reddit.com › r/webdev › what is this "file is a commonjs module" error message?
r/webdev on Reddit: What is this "File is a CommonJS module" error message?
December 13, 2018 - File is a CommonJS module; it may be converted to an ES6 module.ts(80001) var require: NodeRequire (id: string) => any
🌐
Node.js
nodejs.org › api › modules.html
Modules: CommonJS modules | Node.js v25.9.0 Documentation
Files with a .js extension when the nearest parent package.json file contains a top-level field "type" with a value of "commonjs". Files with a .js extension or without an extension, when the nearest parent package.json file doesn't contain a top-level field "type" or there is no package.json in any parent folder; unless the file contains syntax that errors unless it is evaluated as an ES module.
🌐
Medium
electerious.medium.com › from-commonjs-to-es-modules-how-to-modernize-your-node-js-app-ad8cdd4fb662
From CommonJS to ES Modules: How to modernize your Node.js app
March 22, 2021 - Both modules systems are different and not everything can be converted 1:1. Run the tool with the verbose flag to see which files failed: cjs-to-es6 --verbose src/. As already mentioned—Both modules systems are different. It’s impossible to convert all files automatically. While cjs-to-es6 is a big help, it’s still necessary to look at each file individually.
🌐
GitHub
github.com › microsoft › vscode › issues › 50767
Remove "File is a CommonJS module; it may be converted to an ES6 module" suggestion · Issue #50767 · microsoft/vscode
May 30, 2018 - CJS is great modules format when we want to target pre ES2017 implementations without transpilation (it's format that bundles directly with no syntax changes, even to ES3 engines, ESM will always require transpilation step) There is no plan to remove or deprecate CJS in Node.js. ESM is being added there as a second format, and due to richer functionality of CJS (e.g. public access to cache), one may still prefer to write CJS for Node.js programs.
Published   May 30, 2018
Author   medikoo
🌐
YouTube
m.youtube.com › shorts › HZ1rHOJREnk
Nodejs How To Convert CommonJS Modules To -ES Modules ...
Share your videos with friends, family, and the world
Published   August 12, 2022