29

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

1

6 Answers 6

55

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"
}
6
  • Hi, I have a similar problem as well and tried this solution. It somewhat works, but do you know why? stackoverflow.com/questions/77592704/… Commented Dec 3, 2023 at 1:08
  • 1
    Hey, I managed to solve my issue in this way as well, it works because when your project installs dependencies, the package manager first looks at theresolutions field. If there are any versions specified for any dependencies in the resolutions, it will prioritize those versions installed. Commented Dec 11, 2023 at 12:18
  • @Matthew but how did you know those specific versions will resolve it? Commented Dec 29, 2023 at 16:03
  • 1
    It's resolved because these are versions before these modules go full esm.
    – Scoup
    Commented Jan 14 at 13:42
  • Thank you! I was running into this issue for a while! Commented Feb 21 at 22:57
35

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.

7
  • Yes, this is the fix!
    – Cameron
    Commented Feb 1 at 17:32
  • 4
    To me this method only works once, if I restart the program the same issue still occurs, adding "resolutions" in package.json as suggested in the answer below fix it permanently
    – Tyson Z
    Commented Feb 6 at 14:54
  • The same happened to me. I thought it was working ok but then I had to build a container and in the build process popup the same problem. The only fix was to add the "resolutions" in package.json Commented Jul 18 at 6:27
  • This method is useful in my project, which use Vite + Electron
    – aokihu
    Commented Jul 27 at 9:04
  • 1
    Regenerating the entire yarn.lock file can be a bit risky in large projects with many deps. In such cases, you can manually remove the sections of yarn.lock corresponding to the specific dependency you want to discard lockfile details for, then re-run yarn install. Other dependencies besides the target will remain locked. Commented Aug 13 at 15:16
8

I achieved to solve this issue but as a workaround. What I did was to clean Yarn cache, remove yarn.lock, remove node-modules, and I reinstalled all packages from scratch. That's it.

0
7

I added this to package.json

"resolutions": {
  "wrap-ansi": "7.0.0",
  "string-width": "4.1.0"
}

and after that I needed to delete node_modules remove yarn.lock and yarn install again

0

Update: Time later it started to bother me again this issue, so according some blogs found in internet, I decided to discard using yarn and I switched back to npm and it's working now because it seems this issue is related to a bug in yarn. In some places recommend to upgrade yarn 1 to 3 but the upgrading process come up with issues, so I prefer to keep with npm.

-2

I had the same issue when I was running NestJS project with docker and I was able to solve it by deleting the yarn.lock file and never copy it in the app directory in Dockfile.

Here is the example of the line I removed in the Dockfile

COPY --chown=node:node yarn.lock ./

1
  • 1
    This is not a solution. yarn.lock fine is an important part of the application that defines the versions of your dependencies. If you remove it you may get different version of dependencies with each build resulting into undeterministic behavior.
    – scr4bble
    Commented Feb 21 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.