Skip to content
Advertisement

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import when attempting to start Nodejs App locally

I’m caught in a bit of a loop trying to deploy my app to Heroku. My import statements (e.g. import cors from 'cors') seem to prevent the app from launching in production, due to the “Cannot Load ES6 Modules in Common JS” error. Locally it runs just fine.

However, when I attempt to resolve the above error by adding "type": "module" to my package.json I get a whole new set of errors and the app will no longer run locally. I believe this error is due to the way I’m initializing sequelize and associated models but I am unsure. I’d like to resolve this error but need a hand with new syntax for the imports… I think.

Error, package.json and index.js include below.

Error Text

JavaScript

Package.JSON

JavaScript

Index.js

JavaScript

Thoughts or pointers? Thank you!

Advertisement

Answer

In Node.js, import statements are permitted only in ES modules. so, directory imports do not work in Node.js. Read Node.js Documentation.

Advertisement