My typescript compiler is looking in the wrong place for the types for one of my modules (redis), but only when run inside of Docker. Here's the end of the logging information running tsc inside the container:
======== Module name '../classes/range' was successfully resolved to '/app/node_modules/@types/semver/classes/range.d.ts' with Package ID '@types/semver/classes/[email protected]'. ========
... more successful logs ...
node_modules/@types/connect-redis/index.d.ts(22,29): error TS2694: Namespace '"/app/redis"' has no exported member 'RedisClient'.
As you can see, it appears that the tsc command is looking inside of /app/redis for the member RedisClient rather than inside /app/node_modules/@types/redis which does export the type information. The tsc compiler doesn't have any problems when run from my local machine, only when it's being run inside the docker instance. Why is this the case?
Inside docker, the tsc command is being run from my package.json file, which is being run using docker-compose. The docker-compose file looks like this:
version: "3"
services:
api:
image: "typeorm-api:production"
container_name: "typeorm-api"
build:
context: ./
dockerfile: Dockerfile
volumes:
- ./src:/app/src
environment:
ENV: production
PORT: 1234
env_file:
- ".env.production"
# This command will overwrite what is contained in the Dockerfile
command: "npm run start:production"
ports:
- 1234:1234
depends_on:
- redis_server
reverse:
container_name: reverse
hostname: reverse
image: nginx:latest
volumes:
- ${PWD}/nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
- 443:443
redis_server:
image: "redis:alpine"
container_name: redis_server
volumes:
- "./redis/production/data:/var/lib/redis"
expose:
- 6379
The npm run start:production command runs a command that's contained inside my package.json file (which installs my dependencies inside my application on the build step) and looks like this:
"start:production": "tsc --extendedDiagnostics --traceResolution && node dist/index.js"
The part that's failing is the tsc step and gives the error mentioned above.
The development settings work fine. Other files that might be useful in debugging, listed below.
- Full
package.jsonfile:
{
"name": "myapi",
"version": "0.2.1",
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"@types/connect-redis": "^0.0.16",
"@types/cors": "^2.8.10",
"@types/express": "^4.17.11",
"@types/express-session": "^1.17.3",
"@types/ioredis": "^4.22.0",
"@types/node": "^14.14.31",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"eslint": "^7.21.0",
"ts-node": "9.1.1",
"ts-node-dev": "^1.1.6"
},
"dependencies": {
"@types/redis": "^2.8.28",
"apollo-server-express": "^2.21.0",
"bcryptjs": "^2.4.3",
"class-validator": "^0.13.1",
"connect-redis": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-session": "^1.17.1",
"graphql": "^15.5.0",
"ioredis": "^4.23.0",
"pg": "^8.5.1",
"redis": "^3.0.2",
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.1.1",
"typeorm": "0.2.31",
"typescript": "^4.2.2"
},
"scripts": {
"start:development": "ts-node-dev --respawn --poll src/index.ts",
"start:production": "tsc --extendedDiagnostics --traceResolution && node dist/index.js"
}
}
- Full Dockerfile
FROM node:15.4.0-alpine3.10
# Specify that when you create the container, put the application inside the /app container
WORKDIR /app
# Copy our package.json + package-lock.json file into the /app folder (wildcard allows for package.lock.json)
COPY package*.json /app/
RUN npm ci
# Copy source files + compilation configuration, db connection options, etc.
COPY ./src .
COPY tsconfig.json .
COPY ormconfig.js .
COPY modules.d.ts .
# This command will be overwritten to be start:production with our docker-compose.prod.yml file
CMD npm run start:development
EXPOSE 1234