6

我正在 AWS 上部署一个 dockerized typescript 应用程序。当我运行应用程序容器时,我得到一个 ts-node 错误Error: Cannot find module 'typescript',我不清楚,因为typescript它被定义为 devDependency。这些是我的package.json依赖

{
 "name": "app-server",
 "version": "0.0.0",
 "description": "description",
 "author": "Marcello Bardus",
 "license": "MIT",
 "scripts": {
   "build": "tsc -p tsconfig.build.json",
   "format": "prettier --write \"src/**/*.ts\"",
   "start": "ts-node -r tsconfig-paths/register src/main.ts",
   "start:dev": "nodemon",
   "start:debug": "nodemon --config nodemon-debug.json",
   "prestart:prod": "rimraf dist && tsc",
   "start:prod": "node dist/main.js",
   "lint": "tslint -p tsconfig.json -c tslint.json",
   "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": "^5.4.0",
   "@nestjs/core": "^5.4.0",
   "@nestjs/typeorm": "^5.3.0",
   "@types/mongoose": "^5.3.18",
   "elliptic": "^6.4.1",
   "mongoose": "^5.4.14",
   "object-hash": "^1.3.1",
   "pg": "^7.8.0",
   "randomstring": "^1.1.5",
   "reflect-metadata": "^0.1.12",
   "rimraf": "^2.6.2",
   "rxjs": "^6.2.2",
   "sha2": "^1.0.2",
   "typeorm": "^0.2.13",
   "typescript": "^3.0.1"
 },
"devDependencies": {
  "@nestjs/testing": "^5.1.0",
  "@types/express": "^4.16.0",
  "@types/jest": "^23.3.1",
  "@types/node": "^10.7.1",
  "@types/supertest": "^2.0.5",
  "jest": "^23.5.0",
  "nodemon": "^1.18.10",
  "prettier": "^1.14.2",
  "supertest": "^3.1.0",
  "ts-jest": "^23.1.3",
  "ts-loader": "^4.4.2",
  "ts-node": "^7.0.1",
  "tsconfig-paths": "^3.5.0",
  "typescript": "^3.0.1",
  "tslint": "5.11.0"
},
"jest": {
  "moduleFileExtensions": [
    "js",
    "json",
    "ts"
  ],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
  "^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
  }
}

和 Dockerfile

FROM node
WORKDIR /usr/src/app

COPY . /usr/src/app

RUN npm install -g ts-node nodemon
RUN npm install
RUN npm install typescript

EXPOSE 8085

CMD ["nodemon", "."]

我想typescript一旦容器建成,它就会在本地安装,但事实并非如此。提前致谢

4

3 回答 3

10

最后,我让 ts-node 使用 npx 工作:

  1. 全局安装打字稿:

    npm i typescript -g
    
  2. 转到您的项目目录并将 typescript 链接到项目:

    cd <my-project>
    
    npm link typescript
    
  3. 使用 npx 执行 ts-node:

    npx ts-node <your-ts-script>.ts
    
于 2020-12-30T14:22:46.147 回答
3
于 2021-07-19T02:22:10.313 回答
1

如果您打算使用ts-nodenodemon作为应用程序的起点,请在package.json依赖项中明确定义它们。

然后RUN npm install --production

他们将可以在node_modules/.bin/

~/foo/node_modules/.bin

> ls
is-ci@    nodetouch@  rc@      ts-node@         ts-node-transpile-only@
nodemon@  nopt@       semver@  ts-node-script@  ts-script@

然后你可以运行./node_modules/.bin/nodemonnpx nodemon作为你的 Docker CMD

npx

从本地 node_modules/.bin 或中央缓存执行,安装运行所需的任何包。

来源:npx


提示提高你的Dockerfile正确的缓存方式node_modules

COPY package*.json /usr/src/app

请注意,我们只是复制 package.json 文件,而不是复制整个工作目录。这使我们能够利用缓存的 Docker 层。

然后复制应用文件

COPY . /usr/src/app

别忘了.dockerignore node_modules

于 2021-02-03T09:27:09.260 回答