Search code examples
javascripttypescriptvisual-studio-codees6-modulescommonjs

How do I disable ES Module Error's in VSCode?


I have recently made the switch to use Bun as my JS Runtime of choice, and in the switch the relevance of ES Modules vs CommonJS became a moot point because Bun doesn't acknowledge either and it all just works. Unfortunately VSCode doesn't seem to agree and whenever I do something that would work in Bun, but not for Node.JS, it gives me errors such as:

enter image description here

and

enter image description here

I acknowledge that I could simply use @ts-ignore, but that would get out of hand rather quickly. I want to know if there is something I can do to my TSConfig file or my VSCode settings that would rid me of these ESM vs CommonJS errors.

Edit: Changing the package.json to "type": "module" produces many more errors than it fixes.


Solution

  • Following our exchange in the comments section, you should do the following:

    1. Modify your package.json file to add/modify:
    "type": "module"
    
    1. Modify your tsconfig.json file to add/modify:
    "moduleResolution": "bundler"
    

    Explanation

    1. "type": "module" in package.json tells Typescript to interpret .js files as ES Modules (instead of CommonJS by default).
    2. "moduleResolution": "bundler" in tsconfig.json tells Typescript that your bundler is going to take care of the relative import paths and will not require the .js extension that Node.js needs in ESM mode.