2
votes

Command tsc -w -p server watch the server directory compile TypeScript into dist/server folder (dist/server/app.js is the main Node script).

Command nodemon -w dist/server dist/server/app.js watches dist/server folder and reloads dist/server/app.js when something changes.

The problem: if I run both commands in parallel, tsc will take some times but nodemon starts too soon, when dist/server/app.js doesn't exist yet.

concurrently \"tsc -w -p server\" \"nodemon -w dist/server dist/server/app.js\"

On the other hand, if I run the commands sequentially I lost nodemon output (that is, the server output) because tsc will watch for changes and "steal" the console ouput:

tsc -w -p server\ && nodemon -w dist/server dist/server/app.js

I've tested both these strategies with nodemon and npm-run-all, a popular alternative.

Related questions (accepted answer doesn't solve the problem):

1
Another possible set of answers for you: stackoverflow.com/questions/41853422/…cham

1 Answers

0
votes

Adding a delay of 1000ms to nodemon fixed the issue for me.

https://github.com/remy/nodemon#delaying-restarting

nodemon.json

{
  "watch": ["build"],
  "ext": "js",
  "exec": "npm start",
  "delay": 1000
}

package.json

{
  "name": "demo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node build/index.js",
    "ts": "tsc -w",
    "nodemon": "nodemon",
    "code": "concurrently -n ts,nodemon npm:ts npm:nodemon"
  },
  "devDependencies": {
    "concurrently": "^4.1.0",
    "nodemon": "^1.18.9",
    "typescript": "^3.2.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es2017",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "build",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}

npm run code