Search code examples
node.jses6-modules

'Directory import is not supported resolving ES modules' with Node.js


I'm using Node.js v14.13.0.

app.js file:

import database from './database';

database();

database/index.js file:

import mongoose from 'mongoose';

export default connect = async () => {
    try {
        await mongoose.connect('...', { });
    } catch (error) {}
};

In package.json I added "type": "module".

After running the app I get the following error:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/xx/Desktop/Projects/node-starter/src/database' is not supported resolving ES modules imported from /Users/xx/Desktop/Projects/node-starter/src/app.js


Solution

  • With ES6 modules you can not (yet?) import directories. Your import should look like this:

    import database from "./database/index.js"