I have installed TypeORM by using the following command:
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".
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:
{
"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
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
:
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. 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. The easiest solution is on line 9 of the Dockerfile to change from RUN npm install --only=development
to RUN npm install
. This will install all of the dependencies, and the docker container will be able to build properly. 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)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.