简体   繁体   English

找不到模块“@nestjs/typeorm”

[英]Cannot find module '@nestjs/typeorm'

I have installed TypeORM by using the following command:我已经使用以下命令安装了 TypeORM:

Github repository Github 存储库

npm i --save @nestjs/typeorm typeorm

up to date, audited 855 packages in 3s

86 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

but when I want to use @nestjs/typeorm it says "module not found".但是当我想使用@nestjs/typeorm它说“找不到模块”。

main.ts : main.ts

import 'dotenv/config';
import {Logger} from '@nestjs/common';
import {NestFactory} from '@nestjs/core';
import {AppModule} from './app.module';

import {TypeOrm} from '@nestjs/typeorm';

const port = process.env.PORT || 3000;

async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    await app.listen(port);
    Logger.log(`server started on ${port}`, 'Bootstrap');

}

bootstrap();

error:错误:

error TS2307: Cannot find module '@nestjs/typeorm' or its corresponding type declarations.

Here is my "package.json" file:这是我的“package.json”文件:

{
  "name": "---",
  "version": "0.0.1",
  "description": "",
  "author": "---",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "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": "^8.0.0",
    "@nestjs/config": "^1.0.1",
    "@nestjs/core": "^8.0.0",
    "@nestjs/platform-express": "^8.0.0",
    "@nestjs/platform-socket.io": "^8.0.6",
    "@nestjs/typeorm": "^8.0.2",
    "dotenv": "^10.0.0",
    "g": "^2.0.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "typeorm": "^0.2.36"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "^26.0.24",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",

    ...
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

i'm not sure Docker cause happening this error error我不确定Docker导致发生此错误错误

Dockerfile : Dockerfile

FROM node:12.19.0-alpine3.9 AS development

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install glob rimraf

RUN npm install --only=development

COPY . .

RUN npm run build

FROM node:12.19.0-alpine3.9 as production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install --only=production

COPY . .

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/main"]

and docker-compose.yml :docker-compose.yml

version: '3.8'

services:
    dev:
        container_name: nestjs_api_dev
        image: nestjs-api-dev:1.0.0
        build:
            context: .
            target: development
            dockerfile: ./Dockerfile
        command: npm run start:debug
        ports:
            - 3000:3000
            - 9229:9229
        networks:
            - nesjs-network
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        restart: unless-stopped
    prod:
        container_name: nestjs_api_prod
        image: nestjs-api-prod:1.0.0
        build:
            context: .
            target: production
            dockerfile: ./Dockerfile
        command: npm run start:prod
        ports:
            - 3000:3000
            - 9229:9229
        networks:
            - nesjs-network
        volumes:
            - .:/usr/src/app
            - /usr/src/app/node_modules
        restart: unless-stopped

networks:
    nesjs-network:

In the above Dockerfile and in the sample shared by DolDurma, during the development step, only the development dependencies are installed.上述Dockerfile和DolDurma分享的示例中,在development阶段,只安装了开发依赖。 This means that when typescript tries to find things like @nestjs/typeorm , @nestjs/common and @nestjs/core , along with any other packages taht are required to run the build and run the application, it can't, so it throws an error about not being able to find the package or its type declarations.这意味着当打字稿试图找到诸如@nestjs/typeorm@nestjs/common@nestjs/core ,以及运行构建和运行应用程序所需的任何其他包时,它不能,所以它抛出关于无法找到包或其类型声明的错误。 The easiest solution is on line 9 of the Dockerfile to change from RUN npm install --only=development to RUN npm install .最简单的解决方案是在Dockerfile 的第 9 行将RUN npm install --only=development更改为RUN npm install This will install all of the dependencies, and the docker container will be able to build properly.这将安装所有依赖项,并且 docker 容器将能够正确构建。 Then docker compose up will work (the instances above will not run because both bind to port 3000, but you can do docker compose up dev and it should be fine)然后docker compose up将起作用(上面的实例将无法运行,因为两者都绑定到端口 3000,但是您可以执行docker compose up dev并且应该没问题)

It looks like with the sample for DolDurma some extra dependencies still need to be installed, but the container does build and attempts to start with the above modification看起来 DolDurma 的示例仍然需要安装一些额外的依赖项,但是容器确实构建并尝试从上述修改开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM