简体   繁体   English

Typescript:在 Docker 容器中找不到模块 XWZ 或其对应的类型声明

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

Environment: Docker container running nodejs image.环境:Docker容器运行nodejs镜像。

The api works fine. api 工作正常。 The ts-node-dev is running perfectly, no issue with express or other packages. ts-node-dev 运行完美,express 或其他包没有问题。 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.当我尝试使用 ORM 连接到数据库时出现的错误。 I tried two of them: TypeORM and Sequelize.我尝试了其中的两个:TypeORM 和 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.不知道是Typescript编译器的问题还是docker环境的问题。 Because this only happens when I run the app inside the container through docker-compose up .因为这只发生在我通过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我注意到这两个库都使用库内类型声明,我的意思是,没有 @types/sequelize

This is the repository to reproduce the error: https://bitbucket.org/carloseustaquio/test-cicd-docker/src/master/这是重现错误的存储库: https://bitbucket.org/carloseustaquio/test-cicd-docker/src/master/

Dockerfile 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 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 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 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 .尝试npm i @types/sequelize获取模块的类型或使用dts-gen生成声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到模块“。” 或其相应的类型声明 - Cannot find module '.' or its corresponding type declarations 错误:找不到模块“jsonwebtoken”或其对应的类型声明 NODE.JS 和 TYPESCRIPT - ERROR: Cannot find module 'jsonwebtoken' or its corresponding type declarations with NODE.JS and TYPESCRIPT 找不到模块“./App.svelte”或其对应的类型声明 - Cannot find module './App.svelte' or its corresponding type declarations 找不到模块“hardhat/config”或其相应的类型声明 - Cannot find module 'hardhat/config' or its corresponding type declarations 错误 TS2307:找不到模块或其对应的类型声明。 在 Github 操作 - error TS2307: Cannot find module or its corresponding type declarations. on Github Actions src/index.ts:12:22 - 错误 TS2307:找不到模块“./schema.graphql”或其相应的类型声明 - src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations 使用带有 Next.js 的 Typescript 找不到具有相应类型声明的模块 - Module not found with corresponding type declarations using Typescript with Next.js Express.js在Docker容器内找不到模块'opencv' - Express.js Cannot find module 'opencv' inside of a docker container 在打字稿中找不到模块 - Cannot find module in Typescript Nodejs - 找不到模块(Docker) - Nodejs - cannot find module (Docker)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM