Nodejs Debugger II – Typescript Nodemon

Configuring Nodemon on a Node.js server

1. Add Nodemon as devDependency :

  "name": "nodemon",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "node src/server.js"
  },
  "dependencies": {
    "express": "4.15.3"
  },
  "devDependencies": {
    "nodemon": "1.11.0"
  }
}

2. Add the dev command to the package.json file


  "scripts": {
    "start": "node src/server.js",
    "dev": "nodemon --watch src src/server.js"
  }
}

Now run npm run dev and request again the use of the curl command

https://medium.com/front-end-weekly/configuring-nodemon-on-a-node-js-server-da9eed2eeb5

Nodemon is the tool that helps you to develop node.js based applications by automatically restart a node application when file changes in the directory are detected. The nodemon server does not require any additional changes into your code or method of development. The nodemon is a replacement wrapper for the node, to use the nodemon replaces the word node on the command line when executing your script.

Alternatively, nodemon will also look for the primary file specified in your project’s package.json file or the start script. Given either of the examples below, you can then directly call nodemon to start your app in watch mode. We can also write the command in the start script in the package.json file.

{
  "name": "nodemon-exp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3"
  }
}

Now, you need to hit the following command.
npm start

#Advanced Options

--exec: Use –exec to specify the binary to execute the file with. For example, when combined with the ts-node binary, --exec can become useful to watch for changes and run TypeScript files.
--ext: Specify different file extensions to watch. For this switch, provide the comma-separated list of file extensions (e.g.: --ext js,ts).
--delay: By default nodemon waits for 1 second to restart a process when the file changes, but with --delay you can specify a different delay. For example, $ nodemon --delay 3.2for a 3.2 second delay.
--watch: Use the --watch switch to determine multiple directories or files to watch. Add one --watch switch for each list you want to watch. By default the current directory and its subdirectories are viewed, so with --watch you can narrow that to only specific subdirectories or files.
--ignore: Use --ignore to ignore particular files, file patterns or directories.
--verbose:  More verbose output with an information about what file(s) changed to trigger a restart.

#Nodemon Config

Adding configuration switches when running nodemon can get quite complicated. The better solution for the projects that need specific configurations is to specify these configurations in the nodemon.json file.

{
  "watch": ["server"],
  "ext": "ts",
  "ignore": ["*.test.ts"],
  "delay": "3",
  "execMap": {
    "ts": "ts-node"
  }
}

Debug Typescript Project With Nodemon

Note:
1 .We can debug node app using chrome debugger – “chrome://inspect/#devices”
2. Click the link “Open dedicated DevTools for Node”
3. Place the break point into the application source code
4. press Ctrl+O in chrome and find the file name

{
   "name": "server",
   "version": "0.0.1",
   "description": "Awesome project developed with TypeORM.",
   "devDependencies": {
      "@types/express": "^4.17.8",
      "@types/express-session": "^1.17.0",
      "@types/graphql": "^14.5.0",
      "nodemon": "^2.0.6",
      "ts-node": "^9.1.1",
      "typescript": "^4.1.2"
   },
   "dependencies": {
      "express": "^4.17.1",
      "express-session": "^1.17.1",
      "graphql": "^15.4.0"
   },
   "scripts": {
      "start": "nodemon --inspect -r ts-node/register src/index.ts"      
   }
}

If we want to use nodemon configuration as a separate file we can do like this

{
	"verbose": true,
    "debug": false,    
	"ignore": [
		"mochawesome-report",
		"node_modules",
		"./test",
		"**/*.d.ts",
		"*.test.ts",
		"*.spec.ts",
		"fixtures/*",
		"test/**/*",
		"docs/*"
	],
	"events": {
		"restart": "echo \"[Warning] Remember run npm run test b4 push to dev branch !\""
    },    

	"exec": "node --inspect -r ts-node/register ./src/index.ts",
	"watch": ["./src"],
    "ext": "ts, gql",
    "env": {
        "NODE_ENV": "development"
    },
	"inspect": true
}

With ts-node 5.0.0 you no longer pass the –inspect flag the same way. The suggested way is “node –inspect -r ts-node/register path/to/ts”.
For example:

nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register src/app.ts

more sample package.json file reagarding command

"scripts": {
    "start": "npm run start:dev",
    "start:production": "better-npm-run start-prod",
    "start:dev": "better-npm-run start-dev",
    "update-schema": "babel-node ./scripts/updateSchema.js",
    "prettier": "prettier --write \"{*.js,!(node*)**/*.js}\"",
    "build": "babel src -d build --copy-files && npm run build:copy",
    "build:copy": "copyfiles package.json ./build",
    "lint-fix": "eslint --fix ."
  },
  "betterScripts": {
    "start-prod": {
      "command": "node build/server.js",
      "env": {
        "NODE_ENV": "production"
      }
    },
    "start-dev": {
      "command": "nodemon -w src/server.js --exec \"babel-node src/server.js\"",
      "env": {
        "NODE_ENV": "development"
      }
    }
  },

debugging is not possible using –inspect parameter
Why does the node inspector not start when I am using nodemon and ts-node?
nodemon-tutorial-example-from-scratch

Leave a Reply

Your email address will not be published. Required fields are marked *