Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error [ERR_REQUIRE_ESM]: require() of ES Module, node_modules\wrap-ansi\index.js not supported

In a nestjs project I'm facing issues with a library I'm not dicrectly using in my code but it's used by third-party.

stringWidth = require('string-width') in node_modules\wrap-ansi\index.js:2 not supported

Here is package.json dependencies:

"dependencies": {
    "@nestjs-modules/mailer": "^1.9.1",
    "@nestjs/common": "^10.1.2",
    "@nestjs/config": "^3.0.0",
    "@nestjs/core": "^10.1.2",
    "@nestjs/event-emitter": "^2.0.2",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/mapped-types": "^2.0.2",
    "@nestjs/passport": "^10.0.0",
    "@nestjs/platform-express": "^10.1.2",
    "@nestjs/swagger": "^7.1.4",
    "@nestjs/throttler": "^4.0.0",
    "@nestjs/typeorm": "^10.0.0",
    "@ttshivers/automapper-classes": "^8.8.3",
    "@ttshivers/automapper-core": "^8.8.3",
    "@ttshivers/automapper-nestjs": "^8.8.3",
    "bcrypt": "^5.1.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "handlebars": "^4.7.8",
    "nodemailer": "^6.9.7",
    "passport": "^0.6.0",
    "passport-custom": "^1.1.1",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "pg": "^8.11.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.16"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.1.11",
    "@nestjs/schematics": "^10.0.1",
    "@nestjs/testing": "^10.1.2",
    "@types/bcrypt": "^5.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.5.3",
    "@types/node": "20.4.5",
    "@types/nodemailer": "^6.4.13",
    "@types/passport-jwt": "^3.0.8",
    "@types/passport-local": "^1.0.35",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^6.2.0",
    "@typescript-eslint/parser": "^6.2.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "29.6.2",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.1.1",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.2.0",
    "typescript": "^5.1.6",
    "vite": "^4.4.7"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "moduleNameMapper": {
      "@application/(.*)": "<rootDir>/application/$1",
      "@domain/(.*)": "<rootDir>/domain/$1",
      "@infrastructure/(.*)": "<rootDir>/infrastructure/$1"
    }
  }

The latest packages I've installed when I noticed the issue was:

@nestjs-modules/mailer
nodemailer
handlebars
@types/nodemailer

The issue I'm facing running any nest command is this:

stack trace of error here

Based on similar posts, here an here, I tried by adding "type": "module" in package.json but not success. Also, I removed node_modules and reinstall packages (yarn) and this is what I get:

enter image description here

same error: var stringWidth = require('string-width') but in different file: node_modules\wide-align\align.js:2

Thanks for your help

like image 384
Aldemar Cuartas Carvajal Avatar asked Sep 01 '25 17:09

Aldemar Cuartas Carvajal


2 Answers

I have the same issue and this helped me to solve this problem

Add this to your package.json file

"resolutions": {
  "wrap-ansi": "7.0.0",
  "string-width": "4.1.0"
}
like image 198
Matthew Avatar answered Sep 04 '25 09:09

Matthew


I've had the same issue after upgrading the system to macOS Sonoma 14.2.1.

My solution is:

Remove yarn.lock file and yarn install again fixed the issue.

like image 31
Virak Avatar answered Sep 04 '25 11:09

Virak