Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"type": "module" in package.json throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath)

I want to use import in my nodejs project instead of using require. So, I added,

"type": "module"

in my package.json.

import index from './index.js';

in server.js

when I run

node server.js

Error says,

internal/modules/cjs/loader.js:1174
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ....

server.conf.js is pasted below.

import express from 'express';

import http from 'http';
let app = express();
let server = http.createServer(app);

import morgan from 'morgan';
import methodOverride from 'method-override';;
import path from 'path';

let port = process.env.PORT || 4000;
app.use(morgan('dev'));
app.use(methodOverride('X-HTTP-Method-Override'));
let router = express.Router();
import routes from '../app/routes';
routes(app, router, client);
server.listen(port);
console.log(`Wizardry is afoot on port ${port}`);
export {
    app,
    client
};
like image 266
Nafeo Joy Avatar asked May 02 '20 11:05

Nafeo Joy


1 Answers

For my case I downgrade: node-fetch ^3.0.0 → ^2.6.1

Problem solved.

like image 100
codetinker Avatar answered Sep 20 '22 14:09

codetinker