简体   繁体   中英

Typescript: Cannot find module XWZ or its corresponding type declarations in Docker Container

Environment: Docker container running nodejs image.

The api works fine. The ts-node-dev is running perfectly, no issue with express or other packages. the db is also working, I've tested it through workbench.

The error stated when I tryied to use an ORM to connect to the database. I tried two of them: TypeORM and Sequelize. Both fired the same type of error:

error TS2307: Cannot find module 'sequelize' or its corresponding type declarations.

I don't know if it's something with the Typescript compiler or the docker environment. Because this only happens when I run the app inside the container through docker-compose up .

Something that I noticed is that both of these libs use inside-of-lib type declarations, I mean, there are no @types/sequelize

This is the repository to reproduce the error: https://bitbucket.org/carloseustaquio/test-cicd-docker/src/master/

Dockerfile

FROM node:12

RUN mkdir -p /home/node/api/node_modules && chown -R node:node /home

WORKDIR /home/node/api

COPY package.json yarn.* ./

USER node

RUN yarn

COPY --chown=node:node . .

EXPOSE 8080

ENTRYPOINT [ "./init.sh" ]

docker-compose.yaml

version: '3'

services:
  nodejs-app:
    container_name: nodejs-app
    build: '.'
    volumes:
      - .:/home/node/api
      - /home/node/api/node_modules
    networks:
      - app-connect
    ports:
      - '8080:8080'
    depends_on: 
      - database
    environment: 
      - DB_URI=mysql://root:password@database/db?charset=UTF8

  database:
    image: mysql:5.7
    environment: 
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_DATABASE=db
    ports: 
      - 0.0.0.0:7200:3306

networks:
  app-connect:
      driver: bridge

package.json

{
  "name": "test-docker",
  "version": "1.0.0",
  "description": "testing docker envairoment",
  "main": "index.js",
  "scripts": {
    "dev": "ts-node-dev --respawn --ignore-watch node_modules --no-notify index.ts",
    "build": "tsc",
    "start": "node dist/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mysql2": "^2.1.0",
    "sequelize": "^6.3.5"
  },
  "devDependencies": {
    "@types/express": "^4.17.8",
    "@types/node": "12",
    "@types/validator": "^13.1.0",
    "ts-node-dev": "^1.0.0-pre.61",
    "typescript": "^4.0.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "moduleResolution": "node",
    "lib": [
      "es6"
    ],
    "typeRoots": [
      "./node_modules/@types",
      "./src/@types",
    ],
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": ".",
    "strict": true,
    "esModuleInterop": true,
    "noImplicitAny": false,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  }
}

try npm i @types/sequelize to get the types of the modules or generate declarations using dts-gen .

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.

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