0

The tsconfig.json:

{
  "compilerOptions": {
    "target": "es5", 
    "module": "commonjs", 
    "outDir": "./dist",  
    "rootDir": "./src", 
    "strict": true,    
    "moduleResolution": "node", 
    "esModuleInterop": true,   
    "skipLibCheck": true,  
    "forceConsistentCasingInFileNames": true
  }
}

The scrips in the package.json:

  "scripts": {
    "build": "tsc -p tsconfig.json"
  },

There are two files the source: src/index.ts and the output: dist/index.js.

There is nodemon installed.

So I can build the index.ts by command:

npm run build

I can run nodemon which watchs for output dist/index.js changes:

nodemon --inspect-brk dist/index.js

How do I setup nodemon to make it watch for changes in index.ts and make it run build command when index.ts is changed?

mr_blond
  • 1,586
  • 2
  • 20
  • 52
  • Take a look at https://github.com/wclr/ts-node-dev – Nenad Vracar Feb 17 '21 at 12:36
  • In case like this you can use one process that will watch your source files and on change create new build, and another with nodemon that will watch build files and restart when there is a new build. – Nenad Vracar Feb 17 '21 at 12:39
  • @NenadVracar what command to create new build in ts-node-dev would look like? – mr_blond Feb 17 '21 at 14:27
  • In your package.json you could add script `"dev": "ts-node-dev src/index.ts"` assuming `src/index.ts` is a path to your index.ts file. – Nenad Vracar Feb 17 '21 at 14:44
  • And then you just run `npm run dev` in the terminal. – Nenad Vracar Feb 17 '21 at 14:45
  • @NenadVracar yes, It runs .ts, one can see console.log output and so on. But i doesn't make new build. It doesn't renew output .js file. – mr_blond Feb 17 '21 at 15:01
  • Because this is meant to be used only in development and it does not write your output files to disk. When you need production build files you use `tsc` command. – Nenad Vracar Feb 17 '21 at 15:20
  • 1
    Alternative to this would be to create two scripts, one `"watch": "tsc --watch"` and another one `"start": "nodemon --watch dist dist/index.js"` and you can run them i two separate terminals. – Nenad Vracar Feb 17 '21 at 15:40
  • @NenadVracar "watch": "tsc --watch" and "start": "nodemon --inspect-brk dist dist/index.js" work – mr_blond Feb 17 '21 at 16:20

0 Answers0