# javascript – How to have top level await in typescript (switch from commonjs to esnext) without having to change all imports to have .js ending

I would like to have top level await in my typescript nodejs project.

My tsconfig used to look like this:

{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####


And I now switched it to

{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####


I have also added "type": "module" to my package.json. Indeed I now have the ability to do top level awaits however

1. I need to change every import to add the .js file extension
2. For folders where I added an index.ts to export all the modules I could previously just import the folder name. Now I need to import foldername/index.js
3. When I auto add an import with vscode it adds it without the .js

The way it is with commonjs is so elegant – can I have the same behaviour with esnext or keep it some other way while gaining top-level await?

Read more here: Source link