无法获取在停靠平均堆栈上工作的nodemon/ts-node-dev

rvpgvaaj  于 2023-02-18  发布在  Node.js
关注(0)|答案(3)|浏览(118)

Mean Stack是全新的,但是通过学习各种教程,我已经成功地在停靠环境中设置了一个。
我遇到的问题是,要更改TypeScript文件,自动编译和重新启动服务失败,我不确定我在配置中遗漏了什么。我尝试了使用nodemon和ts-node-dev的各种迭代,但都没有成功。
服务器启动并100%地提供页面,但是代码更改不会触发任何事情。
下面是Dockerfile(* 用于Node Express服务器 *),package.json和我的tsconfig.json,如果有人能指出我哪里出错了,那将是一个很大的帮助。我还展示了整个堆栈的docker-compose. yml文件。

    • 停靠文件**
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" ]
    • 包. 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\"",
    • 系统配置json**
// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "pretty": true,
        "sourceMap": true,
        "target": "es6",
        "outDir": "./dist",
        "baseUrl": "./lib"
    },
    "include": [
        "lib/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}
    • 停靠-编写. 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在查看Express容器时记录输出

> 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
xxb16uws

xxb16uws1#

你现在可能已经找到了解决办法。我将把这个答案留作参考。
1.在docker-compose.yml中,确保已经将源代码Map到应用服务。

version: '3'

services:
  ...
  express:
    build: ./node-apis-project
    ports:
      - 3000:3000
    links:
      - database
    volumes:
      - .:/usr/src/app # same as WORKDIR in your Dockerfile
  ...

1.根据nodemon手册,正常的监视机制在一些网络环境中可能会失败(比如运行nodemon的容器在挂载的驱动器上阅读),因此您可能不得不使用轮询,这会有点CPU密集(在我的情况下,CPU利用率为6-10%)
对于nodemon,请使用--legacy-watch

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

对于ts节点设备,请使用--poll

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

3df52oht2#

对于因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中的容器的资源并尝试解除它们的限制.
这为我解决了这个问题,我能够调到下面的资源。

kokeuurv

kokeuurv3#

清理所有卷/映像/容器并重建它们对我来说已经做到了。

#!/bin/bash

echo "Cleaning"
docker-compose stop
docker-compose rm
sudo docker-compose down -v
docker volume ls
docker volume prune
docker image prune
docker ps

重新生成docker-compose up --build

相关问题