title: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import when attempting to start Nodejs App locally
body: <p>I'm caught in a bit of a loop trying to deploy my app to Heroku. My import statements (e.g. <code>import cors from 'cors'</code>) seem to prevent the app from launching in production, due to the &quot;Cannot Load ES6 Modules in Common JS&quot; error. Locally it runs just fine.</p>
  <p>However, when I attempt to resolve the above error by adding <code>&quot;type&quot;: &quot;module&quot;</code> to my <code>package.json</code> I get a whole new set of errors and the app will no longer run locally. I <em>believe</em> 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.</p>
  <p>Error, <code>package.json</code> and <code>index.js</code> include below.</p>
  <p><strong>Error Text</strong></p>
  <pre><code>[nodemon] starting `babel-node src/index.js`
  internal/process/esm_loader.js:74
      internalBinding('errors').triggerUncaughtException(
                                ^
  
  Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/jeff/Clients/Bummer/Code/Server/src/models' is not supported resolving ES modules imported from /Users/jeff/Clients/Bummer/Code/Server/src/index.js
      at finalizeResolution (internal/modules/esm/resolve.js:272:17)
      at moduleResolve (internal/modules/esm/resolve.js:699:10)
      at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
      at Loader.resolve (internal/modules/esm/loader.js:85:40)
      at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
      at ModuleWrap.&lt;anonymous&gt; (internal/modules/esm/module_job.js:51:40)
      at link (internal/modules/esm/module_job.js:50:36) {
    code: 'ERR_UNSUPPORTED_DIR_IMPORT',
    url: 'file:///Users/jeff/Clients/Bummer/Code/Server/src/models'
  }
  [nodemon] app crashed - waiting for file changes before starting...
  </code></pre>
  <p><strong>Package.JSON</strong></p>
  <pre><code>{
    &quot;name&quot;: &quot;bummer&quot;,
    &quot;type&quot;: &quot;module&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;&quot;,
    &quot;main&quot;: &quot;index.js&quot;,
    &quot;scripts&quot;: {
      &quot;start&quot;: &quot;node src/index.js&quot;,
      &quot;dev&quot;: &quot;nodemon --exec babel-node src/index.js&quot;,
      &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
    },
    &quot;keywords&quot;: [],
    &quot;author&quot;: &quot;&quot;,
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;devDependencies&quot;: {
      &quot;@babel/core&quot;: &quot;^7.9.6&quot;,
      &quot;@babel/node&quot;: &quot;^7.8.7&quot;,
      &quot;@babel/preset-env&quot;: &quot;^7.9.6&quot;,
      &quot;nodemon&quot;: &quot;^2.0.4&quot;,
      &quot;sequelize-cli&quot;: &quot;^6.2.0&quot;
    },
    &quot;dependencies&quot;: {
      &quot;cookie-parser&quot;: &quot;^1.4.5&quot;,
      &quot;cors&quot;: &quot;^2.8.5&quot;,
      &quot;dotenv&quot;: &quot;^8.2.0&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;pg&quot;: &quot;^8.2.1&quot;,
      &quot;querystring&quot;: &quot;^0.2.0&quot;,
      &quot;request&quot;: &quot;^2.88.2&quot;,
      &quot;sequelize&quot;: &quot;^6.3.5&quot;,
      &quot;sequelize-auto-migrations&quot;: &quot;^1.0.3&quot;,
      &quot;uuid&quot;: &quot;^8.0.0&quot;
    }
  }
  </code></pre>
  <p><strong>Index.js</strong></p>
  <pre><code>import cors from 'cors';
  import express from 'express';
  import models, { sequelize } from './models';
  // import routes from './routes';
  
  //Initiaze Express
  const app = express();
  const routes = require('./routes');
  
  
  //Helpers for Spotify oAuth
  const cookieParser = require('cookie-parser')
  
  
  // Include Middleware
  app.use(express.static(__dirname + '/public'))
     .use(cors())
     .use(cookieParser())
     .use(express.json())
     .use(express.urlencoded({ extended: true }))
     require('dotenv').config()
  
     
  
  // Include all Models
  app.use((req, res, next) =&gt; {
    req.context = {
      models,
    };
    next();
  });
  
  
  
  // Load Routes from Router Index
  app.use('/', routes);
  
  sequelize.sync().then(() =&gt; {
    app.listen(process.env.PORT, () =&gt; {
      console.log(`Example app listening on port ${process.env.PORT}!`)
    });
  });
  </code></pre>
  <p>Thoughts or pointers? Thank you!</p>