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

Posted

技术标签:

【中文标题】无法让 nodemon/ts-node-dev 在 dockerized 平均堆栈上工作【英文标题】:Cannot get nodemon/ts-node-dev working on dockerized mean stack 【发布时间】:2020-02-26 22:57:56 【问题描述】:

Mean Stack 的新手,但已通过遵循各种教程设法在 dockerized 环境中进行设置。我主要使用https://itnext.io/building-restful-web-apis-with-node-js-express-mongodb-and-typescript-part-1-2-195bdaf129cf中解释的那个

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

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

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

Dockerfile

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

查看 express 容器时的 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
      ...
    

    根据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 --transpileOnly ./lib/server.ts",
    

【讨论】:

对于ts-node-dev--poll 成功了。这不在他们的自述文件中,但在源代码中确实出现了。【参考方案2】:

对于因 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 中分配给容器的资源并尝试取消限制。

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

【讨论】:

以上是关于无法让 nodemon/ts-node-dev 在 dockerized 平均堆栈上工作的主要内容,如果未能解决你的问题,请参考以下文章

无法让 UIButton 在 ViewDidLoad 上淡入

无法让 subl 命令在 OSX 终端中工作 [重复]

无法让 DLookup 函数在 Access 2013 中工作

无法让 Firestore 快速入门在 Python 中工作

无法让 SSL 在 Node.js 应用程序中工作

无法让外部连接在 EF 中工作