Reputation: 1100
I have a NestJs project that is build on docker during gitlab CI.
In local, the dockerfile is building great with :
docker build -t server .
But during gitbal CI, with the same docker and same command, it crashes. Especially with @nestjs/common. ( I learned that docker is made to works the same way on every environment. )
Here is my Dockerfile :
FROM node:17-stretch-slim As builder
WORKDIR /app
COPY *.json *.ts ./
RUN npm install [email protected] --location=global
RUN npm install
COPY . .
ENV NODE_ENV=production
# Some custom processes
RUN npm run build:prod
FROM node:17-alpine
WORKDIR /app
COPY . ./
RUN npm install [email protected] --location=global
RUN npm install <-------------------------------------------- THE FAIL IS HERE
COPY --from=builder /app/dist ./
EXPOSE 3000
CMD npm run launch:production
And here is the error :
Step 19/22 : RUN npm install
---> Running in b009e097d4ae
npm WARN deprecated [email protected]: This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in.
> [email protected] postinstall
> rimraf dist && npm run build
> [email protected] prebuild
> rimraf dist
> [email protected] build
> nest build
node_modules/@nestjs/common/index.d.ts:10:15 - error TS2307: Cannot find module './serializer' or its corresponding type declarations.
10 export * from './serializer';
~~~~~~~~~~~~~~
node_modules/@nestjs/common/index.d.ts:11:15 - error TS2307: Cannot find module './services' or its corresponding type declarations.
11 export * from './services';
~~~~~~~~~~~~
node_modules/@nestjs/common/index.d.ts:12:15 - error TS2307: Cannot find module './utils' or its corresponding type declarations.
12 export * from './utils';
~~~~~~~~~
node_modules/@nestjs/common/interfaces/nest-application-context-options.interface.d.ts:1:41 - error TS2307: Cannot find module '../services/logger.service' or its corresponding type declarations.
1 import { LoggerService, LogLevel } from '../services/logger.service';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/interfaces/nest-application-context.interface.d.ts:2:41 - error TS2307: Cannot find module '../services/logger.service' or its corresponding type declarations.
2 import { LoggerService, LogLevel } from '../services/logger.service';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/index.d.ts:3:15 - error TS2307: Cannot find module './parse-bool.pipe' or its corresponding type declarations.
3 export * from './parse-bool.pipe';
~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/index.d.ts:4:15 - error TS2307: Cannot find module './parse-int.pipe' or its corresponding type declarations.
4 export * from './parse-int.pipe';
~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/index.d.ts:5:15 - error TS2307: Cannot find module './parse-float.pipe' or its corresponding type declarations.
5 export * from './parse-float.pipe';
~~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/index.d.ts:6:15 - error TS2307: Cannot find module './parse-enum.pipe' or its corresponding type declarations.
6 export * from './parse-enum.pipe';
~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/index.d.ts:7:15 - error TS2307: Cannot find module './parse-uuid.pipe' or its corresponding type declarations.
7 export * from './parse-uuid.pipe';
~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/index.d.ts:8:15 - error TS2307: Cannot find module './validation.pipe' or its corresponding type declarations.
8 export * from './validation.pipe';
~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/common/pipes/parse-array.pipe.d.ts:3:55 - error TS2307: Cannot find module './validation.pipe' or its corresponding type declarations.
3 import { ValidationPipe, ValidationPipeOptions } from './validation.pipe';
~~~~~~~~~~~~~~~~~~~
node_modules/@nestjs/core/injector/instance-loader.d.ts:1:10 - error TS2305: Module '"@nestjs/common"' has no exported member 'Logger'.
1 import { Logger } from '@nestjs/common';
~~~~~~
node_modules/@nestjs/core/nest-application-context.d.ts:1:35 - error TS2305: Module '"@nestjs/common"' has no exported member 'LoggerService'.
1 import { INestApplicationContext, LoggerService, LogLevel, ShutdownSignal } from '@nestjs/common';
~~~~~~~~~~~~~
node_modules/@nestjs/core/nest-application-context.d.ts:1:50 - error TS2305: Module '"@nestjs/common"' has no exported member 'LogLevel'.
1 import { INestApplicationContext, LoggerService, LogLevel, ShutdownSignal } from '@nestjs/common';
I tried to force RUN npm unsinstall @nestjs/common && npm install @nestjs/common without any changes. I have redone all the step with cache cleared on gitlab etc.
Upvotes: 0
Views: 345
Reputation: 1
try this
FROM node:18-alpine AS development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:alpine as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=prod
COPY . .
COPY --from=development /usr/src/app/dist ./dist
CMD ["node", "dist/main"]
After in your compose try this
version: '3.8'
services:
dev:
container_name: service-dev
image: myimage:1.0.0
build:
context: .
target: development
dockerfile: ./Dockerfile
command: npm run start:dev
ports:
- 3000:3000
networks:
- nesjs-network
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
- /usr/src/app/dist
restart: unless-stopped
networks:
nesjs-network:
I hope I have been helpful
Upvotes: 0
Reputation: 394
its hard to do without linking your libs setup..
just so you can be sure you have it correctly...
nest g library...
inside your libs / common is a directory filled with your common libraries...
+libs
-common
--src
---package1
---package2
---index.ts
inside index ts you are exporting the various modules members of the same directory... it looks like this.
export * from './database/database.module';
export * from './database/abstract.repository';
export * from './database/abstract.schema';
export * from './rmq/rmq.service';
export * from './rmq/rmq.module';
export * from './auth/auth.module';
export * from './auth/jwt-auth.guard';
export * from './constants/ServicePorts';
now when you want to use the common libraries you are doing so as followed.
import { RmqModule } from '@app/common';
import { Logger } from '@app/common'
Upvotes: 0