#Cannot run jest test, got Error [ERR_REQUIRE_ESM]:

1 messages · Page 1 of 1 (latest)

abstract moth
#

I cannot run any test command. They return same error, this is the first time i got this kind of error. I have tried config the jest but no use.
The error is:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\GITHUB\necter-backend\node_modules\wrap-ansi\index.js
require() of ES modules is not supported.
require() of D:\GITHUB\necter-backend\node_modules\wrap-ansi\index.js from D:\GITHUB\necter-

Here the package.json

{
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^9.4.2",
    "@nestjs/config": "^2.3.2",
    "@nestjs/core": "^9.4.2",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/mapped-types": "*",
    "@nestjs/mongoose": "^9.2.2",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-express": "^9.4.2",
    "@nestjs/platform-socket.io": "^9.4.2",
    "@nestjs/schedule": "^3.0.0",
    "@nestjs/swagger": "^6.3.0",
    "@nestjs/websockets": "^9.4.2",
    "@types/passport-jwt": "^3.0.8",
   ...
  },
  "devDependencies": {
    "@nestjs/testing": "^10.0.4",
    "@types/jest": "^29.5.1",
    "@types/node": "^20.2.4",
    "@types/supertest": "^2.0.12",
    "@types/validator": "^13.7.17",
    "jest": "^29.5.0",
    "nodemon": "^2.0.22",
    "prettier": "^2.8.8",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "tslint": "6.1.3",
    "typescript": "^5.0.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  },
  "engines": {
    "node": ">=14.20.1"
  }
}
ornate coral
#

Does it say which module you're trying to require that is ESM?

abstract moth
# ornate coral Does it say which module you're trying to require that is ESM?

I think is wrap-ansi
Full version of errror log

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\GITHUB\necter-backend\node_modules\wrap-ansi\index.js
require() of ES modules is not supported.
require() of D:\GITHUB\necter-backend\node_modules\wrap-ansi\index.js from D:\GITHUB\necter-backend\node_modules\cliui\build\index.cjs is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\GITHUB\necter-backend\node_modules\wrap-ansi\package.json.

    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1131:13)
    at Module.load (internal/modules/cjs/loader.js:979:32)
    at Function.Module._load (internal/modules/cjs/loader.js:819:12)
    at Module.require (internal/modules/cjs/loader.js:1003:19)
    at require (internal/modules/cjs/helpers.js:107:18)
    at Object.<anonymous> (D:\GITHUB\necter-backend\node_modules\cliui\build\index.cjs:293:14)
    at Module._compile (internal/modules/cjs/loader.js:1114:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1143:10)
    at Module.load (internal/modules/cjs/loader.js:979:32)
ornate coral
#

Oh interesting. I'm pretty sure that's a package from @nestjs/cli and wouldn't expect there to be a break in it

abstract moth
#

I think i should upgrade the cli

ornate coral
#

What's your CLI version?

abstract moth
#

9.1.5 current

ornate coral
#

That should be fine. Any chance you can provide access to your repo?

abstract moth
#

This is private project, but i can share some part of it

#

which one do you need

ornate coral
#

I would need a way to reproduce the error

abstract moth
#

Well, i error when i ran the test script

#

there nothing more config

ornate coral
#

Great then you should be able to reproduce this without the private parts, right?

abstract moth
#

i think this is enough to reproduce the error

ornate coral
#

I can't even install the base repo. Something in node-gyp is 404ing

#

Let me downgrade my node version. Might be an issue with v20

#

Yep, node 20 issue. Weird

abstract moth
#

18.13 and 14.21 is fine

ornate coral
#

All right, so install, and just run test?

abstract moth
#

Yeah

ornate coral
#

Error: no tests found

#
[email protected] test ~
> jest

No tests found
In ~/src
  31 files checked.
  testMatch:  - 31 matches
  testPathIgnorePatterns: /node_modules/ - 31 matches
  testRegex: .spec.ts$ - 0 matches
Pattern:  - 0 matches
 ELIFECYCLE  Test failed. See above for more details.
abstract moth
ornate coral
#
  1. those are e2e tests, and for that test:e2e should be the ran script
  2. there's alreayd an e2e spec file
  3. I get this error instead of the one you're getting:
[email protected] test:e2e ~
> jest --config ./test/jest-e2e.json

ts-jest[config] (WARN) Unable to find the root of the project where ts-jest has been installed.
FAIL test/app.e2e-spec.ts
  ● Test suite failed to run

    TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
    test/app.e2e-spec.ts:20:8 - error TS2339: Property 'get' does not exist on type 'SuperTest<Test>'.

    20       .get('/')
              ~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.484s
Ran all test suites.
 ELIFECYCLE  Command failed with exit code 1.
abstract moth
#

run e2e script but same error

abstract moth
ornate coral
#

Even with your package.json I'm not getting that error

abstract moth
#

I will tried to delete the nodemoduke

ornate coral
#

So what you're calling a minimum reproduction isn't. I would need an actual reproduction to help debug this further

abstract moth
#

Okay, let me see what i can do to give u some code

#

weird, after delete all resource in /src no error was throw