找不到模块'@nestjs/typeorm'
Posted
技术标签:
【中文标题】找不到模块\'@nestjs/typeorm\'【英文标题】:Cannot find module '@nestjs/typeorm'找不到模块'@nestjs/typeorm' 【发布时间】:2021-01-18 10:46:47 【问题描述】:我已经使用以下命令安装了 TypeORM:
Github repository
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
但是当我想使用@nestjs/typeorm
时,它会显示“找不到模块”。
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 TS2307: Cannot find module '@nestjs/typeorm' or its corresponding type declarations.
这是我的“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"
我不确定Docker
导致发生此错误错误
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"]
和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:
【问题讨论】:
不确定是否有帮助。我将您的文件与我的文件进行了比较,看来您需要安装@nestjs/config 而不是nestjs-config; 为什么只在 docker 容器的DEVELOPMENT
步骤中安装开发依赖项?
或许有帮助的第一步是在本地环境中复现以排除docker环境导致这种情况的可能性
@xiao 你对,你可以在 GitHub 上签出这个存储库github.com/pishguy/nest-sample
【参考方案1】:
在上述 Dockerfile 和 DolDurma 共享的示例中,在development
步骤中,仅安装了开发依赖项。这意味着,当 typescript 尝试查找诸如 @nestjs/typeorm
、@nestjs/common
和 @nestjs/core
之类的内容以及运行构建和运行应用程序所需的任何其他包时,它不能,因此它会引发关于无法找到包或其类型声明。最简单的解决方案是在line 9 of the Dockerfile 上从RUN npm install --only=development
更改为RUN npm install
。这将安装所有依赖项,并且 docker 容器将能够正确构建。然后docker compose up
将起作用(上面的实例将无法运行,因为它们都绑定到端口3000,但您可以使用docker compose up dev
,应该没问题)
看起来 DolDurma 的示例仍然需要安装一些额外的依赖项,但容器确实构建并尝试从上述修改开始
【讨论】:
以上是关于找不到模块'@nestjs/typeorm'的主要内容,如果未能解决你的问题,请参考以下文章