Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error [ERR_REQUIRE_CYCLE_MODULE]: Cannot require() ES Module...but I'm not

Trying to run my node app, with node main.js, but getting this error: Error [ERR_REQUIRE_CYCLE_MODULE]: Cannot require() ES Module. I have also tried node --experimental-specifier-resolution=node main.js.

My main.js file imports things from './schema/index.js' using regular esm syntax, like: import { importName } from './schema/index.js'. I am not using require, so not sure why it's accusing me of that.

Full error shown below.

What am I doing wrong here?

→ node main.js
node:internal/modules/esm/loader:315
        throw new ERR_REQUIRE_CYCLE_MODULE(message);
              ^

Error [ERR_REQUIRE_CYCLE_MODULE]: Cannot require() ES Module /path/to/app/schema/index.js in a cycle. (from /path/to/app/noop.js)
    at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:315:15)
    at loadESMFromCJS (node:internal/modules/cjs/loader:1414:24)
    at Module._compile (node:internal/modules/cjs/loader:1547:5)
    at Object..js (node:internal/modules/cjs/loader:1677:16)
    at Module.load (node:internal/modules/cjs/loader:1318:32)
    at Function._load (node:internal/modules/cjs/loader:1128:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16) {
  code: 'ERR_REQUIRE_CYCLE_MODULE'
}

Node.js v22.12.0

PS. There is no file called noop.js, and I have no idea what that part means!! Has anyone seen this before?

package.json:

{
  "name": "project-name",
  "version": "0.0.1",
  "dependencies": {},
  "packageManager": "[email protected]",
  "module": "./main.js",
  "type": "module",
  "main": "./main.js"
}
like image 849
nerdlinger Avatar asked Sep 17 '26 20:09

nerdlinger


1 Answers

I've had the same problem, at first I thought it was about a PR which they have on a similar bug (https://github.com/nodejs/node/issues/57172 )

but then after 1h I've seen I was importing like this:

import { sayHello, addNumbers } from '../app'; //without the .js extension

which always resulted in this error although I never used require anywhere

Cannot require() ES Module C:\...\test-profiles.js in a cycle.

and then I've changed it to:

import { sayHello, addNumbers } from '../app.js'; //with the .js extension

and it stated running the tests with mocha test-profiles.js. Maybe it'll help someone.

More info here about a difference between CommonJS( require and module.exports) vs ESM (imports and exports ) https://blog.appsignal.com/2024/12/11/a-deep-dive-into-commonjs-and-es-modules-in-nodejs.html

like image 70
Gabz Avatar answered Sep 19 '26 09:09

Gabz