无法让 nodemon/ts-node-dev 在 dockerized 平均堆栈上工作

Cannot get nodemon/ts-node-dev working on dockerized mean stack

Mean Stack 的新手,但已按照各种教程设法在 docker 化环境中设置了一个。我主要使用 https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf

中解释的那个

我遇到的问题是自动编译对 TypeScript 文件的更改并重新启动服务失败,我不确定配置中缺少什么。我已经尝试使用 nodemon 和 ts-node-dev 进行各种迭代但没有成功。

服务器启动并 100% 提供页面,但代码更改未触发任何内容。

下面是 Docker 文件( 用于 Node Express 服务器 ),package.json 和我的 tsconfig.json,如果有人可以指出我哪里出错了,这将是一个很大的帮助。我还展示了整个堆栈的 docker-compose.yml 文件。

Docker文件

FROM node:latest

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
RUN npm install --save body-parser express mongoose
RUN npm install --save nocache
RUN npm install --save nodemon typescript ts-node ts-node-dev
RUN npm install --save-dev tsc-watch

# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000 27017

# Also tried starting with 'dev'
CMD [ "npm", "run", "prod" ]

package.json

{
  "name": "apis-project",
  "version": "1.0.0",
  "description": "https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "dev": "ts-node ./lib/server.ts",
    "start": "nodemon ./dist/server.js",
    "prod": "npm run build && npm run start"
  },
  "keywords": [
    "nodejs",
    "typescript"
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/express": "^4.17.1",
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "mongoose": "^5.7.7",
    "nodemon": "^1.19.4"
  },
  "devDependencies": {
    "ts-node-dev": "^1.0.0-pre.43"
  }
}


我在脚本部分尝试过的其他变体是:

    "dev2": "ts-node-dev --respawn --transpileOnly ./lib/server.ts",
    "dev3": "nodemon --watch 'lib/*.ts' --exec 'ts-node' lib/server.ts",
    "dev5": "tsc-watch ./lib/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",
    "dev6": "tsc-watch ./lib/*.ts --outDir ./dist --onSuccess \"node ./dist/server.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",
    "dev7": "tsc-watch ./lib/server.ts --outDir ./dist --onSuccess \"node ./dist/server.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",
    "x-compile": "tsc && node ./dist/server.js",
    "x-dev": "./node_modules/nodemon/bin/nodemon.js -e ts  --exec \"npm run x-compile\"",

tsconfig.json

// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "pretty": true,
        "sourceMap": true,
        "target": "es6",
        "outDir": "./dist",
        "baseUrl": "./lib"
    },
    "include": [
        "lib/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

docker-compose.yml

version: '3' # specify docker-compose version

# Define the services/containers to be run
services:
#  angular: # name of the first service
#    hostname: localhost
#    build: serviceangular # specify the directory of the Dockerfile
#    ports:
#      - 4200:4200 # specify port forewarding
#

  express: #name of the second service
    build: ./node-apis-project # specify the directory of the Dockerfile
    ports:
      - 3000:3000 #specify ports forwarding
    links:
      - database

  database: # name of the third service
    image: mongo # specify image to build container from
    ports:
      - 27017:27017 # specify port forewarding
    volumes:
      - ./data/mongo:/data/db

volumes:
  data:
    external: true

Docker 查看快递容器时记录输出

> apis-project@1.0.0 prod /usr/src/app
> npm run build && npm run start


> apis-project@1.0.0 build /usr/src/app
> tsc


> apis-project@1.0.0 start /usr/src/app
> nodemon ./dist/server.js

[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): lib/**/*
[nodemon] watching extensions: js
[nodemon] starting `node ./dist/server.js`
(node:62) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:62) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
Express server listening on port 3000

您现在可能已经找到了解决方案。我会留下这个答案作为参考。

  1. docker-compose.yml中,确保您已经将源代码映射到应用服务。你的情况:

    version: '3'
    
    services:
      ...
      express:
        build: ./node-apis-project
        ports:
          - 3000:3000
        links:
          - database
        volumes:
          - .:/usr/src/app # same as WORKDIR in your Dockerfile
      ...
    
  2. 根据 nodemon manual,正常的监视机制可能会在某些网络环境中失败(例如容器 运行 nodemon 读取已安装的驱动器)因此您可能必须使用轮询有点 CPU 密集(在我的情况下为 6-10% CPU 利用率)

    对于nodemon,使用--legacy-watch:

        "dev3": "nodemon --legacy-watch --watch 'lib/*.ts' --exec 'ts-node' lib/server.ts",
    

    对于 ts-node-dev,使用 --poll:

        "dev2": "ts-node-dev --poll --respawn --transpile-only ./lib/server.ts",
    

对于因 Kubernetes + ts-node-dev 失败并发送类似以下错误而陷入此问题的任何其他人:

[INFO] 09:29:47 ts-node-dev ver. 1.1.1 (using ts-node ver. 9.1.1, typescript ver. 4.1.3)
 
npm ERR! path /app
 
npm ERR! command failed
 
npm ERR! signal SIGKILL
 
npm ERR! command sh -c ts-node-dev src/index.ts

检查您在部署 yaml 中分配给容器的资源并尝试解除限制。

这为我解决了这个问题,我能够在这之后收听资源。