BlackH3art
BlackH3art

Reputation: 596

Cannot find module '@nestjs/core' or its corresponding type declarations

Couple of weeks ago I installed NestJS globally on my computer. Now I'm coming back to that, to start learning.

So I created new project with comand:

nest new ./

It generated all the files, but when I try to run this application with command:

nest start

I expected this template project to start but there are errors with importing paths? Is this due to version of my installed Nest or something?

Errors on the screen: enter image description here

Version of my Nest: 7.5.6

Package.json:

  "dependencies": {
    "@nestjs/common": "^7.6.13",
    "@nestjs/core": "^7.6.13",
    "@nestjs/platform-express": "^7.6.13",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.6"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.6",
    "@nestjs/schematics": "^7.2.7",
    "@nestjs/testing": "^7.6.13",
    "@types/express": "^4.17.11",
    "@types/jest": "^26.0.20",
    "@types/node": "^14.14.31",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.15.2",
    "@typescript-eslint/parser": "^4.15.2",
    "eslint": "^7.20.0",
    "eslint-config-prettier": "^8.1.0",
    "eslint-plugin-prettier": "^3.3.1",
    "jest": "^26.6.3",
    "prettier": "^2.2.1",
    "supertest": "^6.1.3",
    "ts-jest": "^26.5.2",
    "ts-loader": "^8.0.17",
    "ts-node": "^9.1.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.1.5"
  },

Upvotes: 26

Views: 74452

Answers (9)

Дима
Дима

Reputation: 1

Русский: После того как был создан проект с помощью "@nest/cli" необходима инициализация "npm" и делается она таким способом: npm i. либо npm --install English: After the project was created using "@nest/cli", initialization of "npm" is necessary and it is done in this way:npm i. or npm --install

Рад был помочь.

Upvotes: -1

iliaos
iliaos

Reputation: 83

I've encountered this issue today. The reason were changes recently made to yarn.lock, which led to some module resolution issues for nestjs (other apps were just fine). Rebuilding yarn.lock did not help, neither did ensuring consistent versions between @nestjs/* packages or running reinstalls.

I had to restore yarn.lock to the last working version and run yarn install to bring it to the up-to-date state. Then it all worked.

*We have a big monorepo (yarn-3 workspaces, pnp) with a load of apps and shared packages.

**I am still wandering what is the reason behind it. Looks like some initial setup (happening when adding nestjs app to monorepo) set up something extra, something which yarn-3.6.0 with pnp workspaces monorepo does not handle...

Upvotes: 0

Yogesh Umesh Vaity
Yogesh Umesh Vaity

Reputation: 48109

I had the same issue. The following two commands fixed it:

npm install @nestjs/common
npm install @nestjs/core

These commands make sure you are using the lastest versions of those packages.

Sometimes you may also get error: Cannot find module '@nestjs/mapped-types' or its corresponding type declarations.

In that case, do:

npm install @nestjs/mapped-types

Upvotes: 26

Radim Šafrán
Radim Šafrán

Reputation: 598

I had the same problem and none of the solutions above helped me.

My solution:

  1. Open "package.json" and find "@nestjs/core": "X.X.X",
  2. Uninstall all packages whose name starts with "@nestjs/..."

(Uninstall only those that have the same version as "@nestjs/core": 9.1.1)

npm uninstall @nestjs/core @nestjs/common @nestjs/microservices @nestjs/platform-express
  1. Install packages again

(Include version after @)

npm i @nestjs/[email protected] @nestjs/[email protected] @nestjs/[email protected] @nestjs/[email protected]*

Upvotes: 2

redsd
redsd

Reputation: 101

After running npm install, restarting Visual studio code did the trick for me.

Upvotes: 2

Masudur Rahman
Masudur Rahman

Reputation: 65

Use npm or Yarn version 1.Yarn version 3 was the problem for me. It by default does not create node_modules folder. It uses different approach. Which causes the problem.

Upvotes: 0

zemil
zemil

Reputation: 5066

I've stuck with this problem when I built a nodejs application (like, express, nestjs, etc.)

So when you build a nodejs app, the build(dist) folder just contains your code, not node_modules.

You can copy node modules to a build folder, like:

cp package.json build/package.json && cd build && npm install --only=production

Or try other solutions

Upvotes: 4

Saad Khawaja
Saad Khawaja

Reputation: 94

Run this command in terminal "npm install --save @nestjs/config"

Upvotes: 0

Paulo
Paulo

Reputation: 684

Run

npm i --save @nestjs/config

Reference: https://docs.nestjs.com/techniques/configuration

Upvotes: 4

Related Questions