When you set your type
to "module" in your package.json
file you enable ESM modules in your project.
{
"type": "module"
}
But this means that Node gets strict about how it locates your import files (Module Resolution). It requires file extensions on all your import statements. If you want to import a typescript (.ts) file, and you do this
import blah from './blah.ts'; // Wrong
You will get an error because typescript inconveniently removes the .ts extension from this import statement when it compiles this file. It will compile without errors, but then fail at runtime because node needs that file extension. If you instead omit the extension and do this:
import blah from './blah'; // Wrong
Typescript will leave the extension off and compile it just fine but node will again complain at runtime. It turns out you can just import .ts files with a .js extension and Typescript will compile it and leave the extension alone.
import blah from './blah.js'; // Correct
Also try setting module
and moduleResolution
to "Node16"
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16",
...
}
moduleResolution
tonode
in tsconfig – Barnstorm