nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.66k stars 7.63k forks source link

Unable to run application after install @nestjs/platform-express #13298

Closed vfxproForex closed 8 months ago

vfxproForex commented 8 months ago

Is there an existing issue for this?

Current behavior

After installing the package and rerun yarn start:dev, the will refuse to start and output the following: const stringWidth = require('string-width');

the full output is the following:

$ nest start --watch --reset-cache
/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/wrap-ansi/index.js:2
const stringWidth = require('string-width');
                    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/string-width/index.js from /Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/wrap-ansi/index.js not supported.
Instead change the require of /Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/string-width/index.js in /Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/wrap-ansi/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/wrap-ansi/index.js:2:21)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/inquirer/lib/utils/screen-manager.js:4:18)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/inquirer/lib/prompts/base.js:14:23)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/inquirer/lib/prompts/list.js:11:14)
    at promptModule.restoreDefaultPrompts (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/inquirer/lib/inquirer.js:61:33)
    at inquirer.createPromptModule (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/inquirer/lib/inquirer.js:72:16)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/inquirer/lib/inquirer.js:84:28)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/@nestjs/cli/lib/utils/project-utils.js:4:18)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/@nestjs/cli/actions/generate.action.js:9:25)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/@nestjs/cli/actions/index.js:19:14)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/@nestjs/cli/commands/command.loader.js:5:19)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/@nestjs/cli/commands/index.js:17:14)
    at Object.<anonymous> (/Users/tony/workspace/freelance/kurisani/server-app/ftx-server/node_modules/@nestjs/cli/bin/nest.js:5:20) {
  code: 'ERR_REQUIRE_ESM'
}

Minimum reproduction code

git@github.com:vfxproForex/ftx-server.git

Steps to reproduce

  1. npx nestjs new test-app
  2. yarn add @nestjs/platform-express express express-session
  3. yarn add -D @types/express @types/express-session
  4. yarn start:dev

Expected behavior

The server is expected to run normally

Package

Other package

non

NestJS version

10.0.0

Packages versions

  "dependencies": {
    "@apollo/server": "^4.10.0",
    "@as-integrations/fastify": "^2.1.1",
    "@fastify/secure-session": "^7.1.0",
    "@nestjs-modules/mailer": "^1.11.0",
    "@nestjs/apollo": "^12.1.0",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.2.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/graphql": "^12.1.1",
    "@nestjs/microservices": "^10.3.3",
    "@nestjs/mongoose": "^10.0.4",
    "@nestjs/platform-express": "^10.3.3",
    "@nestjs/platform-fastify": "^10.3.3",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.1",
    "connect-redis": "^7.1.1",
    "cookie-parser": "^1.4.6",
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "express-session": "^1.18.0",
    "fastify": "^4.26.1",
    "graphql": "^16.8.1",
    "graphql-tools": "^9.0.1",
    "ioredis": "^5.3.2",
    "mongodb": "^6.3.0",
    "mongoose": "^8.2.0",
    "nodemailer": "^6.9.10",
    "reflect-metadata": "^0.2.0",
    "rxjs": "^7.8.1",
    "uuid": "^9.0.1"
  },

Node.js version

20.9.0

In which operating systems have you tested?

Other

sdsd

micalevisk commented 8 months ago

I believe that you just need to remove the lock file and install everything again

Also, instead of using nest globally, I'd suggest you to use it once like this: npx @nestjs/cli new and then use the local one (yarn nest generate ...)

Tony133 commented 8 months ago

I too have had the same problem these weeks working with yarn. The problem, from what I understand, is yarn, removing the yarn.lock file and doing the yarn installation should solve it. it also happens with projects already created after downloading a repository and doing the yarn installation.

With pnpm this problem does not occur even with versions of yarn higher than v1.22.21, I think it is a problem with 1.22.21.

vfxproForex commented 8 months ago

I jsut tried the solution and i can also confirm that yarn is the problem