'Directory import is not supported resolving ES modules' with Node.js
With ES6 modules you can not (yet?) import directories. Your import should look like this:
import database from "./database/index.js"
What happens here is that Node mandates using an extension in import
statements and also states,
Directory indexes (e.g.
'./startup/index.js'
) must also be fully specified.
Your import database from './database';
statement doesn't specify the index.js
. You can add index.js
as suggested in another answer, but that approach doesn't look elegant in TypeScript projects, when you'd end up importing a .js
file from a .ts
one.
You can change this Node extension resolution behavior by passing the --experimental-specifier-resolution=node
flag. This will work and will keep your code unchanged:
app.js
import database from './database';
database();
Run as: node --experimental-specifier-resolution=node app.js
.
A Node developer admitted that the documentation wasn't that clear.