nodemon: 'ts-node' is not recognized as an internal or external command

nodemon ./src/index.ts

[nodemon] 2.0.14 [nodemon] to restart at any time, enter rs [nodemon] watching path(s): . [nodemon] watching extensions: ts,json [nodemon] starting ts-node ./src/index.ts ‘ts-node’ is not recognized as an internal or external command, operable program or batch file. [nodemon] app crashed - waiting for file changes before starting…

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 10
  • Comments: 16 (3 by maintainers)

Commits related to this issue

Most upvoted comments

In order to fix this issue, you could install ts-node globally :

npm install -g ts-node

https://stackoverflow.com/questions/44764004/ts-node-is-not-recognized-as-an-internal-or-external-command-operable-program

I have the same problem. Trying different ways to solve this issue i found a solution creating a new file in the root directory for the config of nodemon. Now i can be able to run my project without any problem

The config file called nodemon.json have this inside:

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/", "build/"],
  "watch": ["/index.ts"],
  "execMap": {
    "ts": "node -r ts-node/register"
  },
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js,json,ts"
}

And in my package.json, my dev script call the config file for nodemon like this: "dev": "nodemon --config nodemon.json index.ts",

For the moment, that works for me, but i will try to find a different way to implement nodemon, without having the necessity of create a config file to call it.

I investigated the issue and found the root cause.

The problem occurs because of these lines in run.js.

  const spawnOptions = {
    env: Object.assign({}, process.env, options.execOptions.env, {
      PATH: binPath + ':' + process.env.PATH,
    }),
    stdio: stdio,
  };

This is pre-pending the local .bin directory to the PATH, so that locally installed packages may be found.

When using npm run, npm itself is already doing this, pre-pending the correct .bin directories to the path.

In npm@6 the incoming PATH looks like this.

C:\Program Files\Nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;
C:\Work\project\node_modules\.bin;
... rest of the system path

From npm@7, the first part (node-gyp-bin) is no longer passed.

C:\Work\project\node_modules\.bin;
... rest of the system path

nodemon is modifying the incoming PATH variable but using the wrong path separator : (on Windows it would be ;). The result is that the prepended path “corrupts” the first element in the incoming PATH variable, which in npm@7 and later happens to be the local .bin directory. The child cmd shell is spawned without the local .bin directory in its lookup path, so it cannot find the local executables (like ts-node).

I cannot contribute a fix right now, but the solutions should be simple. Just use path.delimiter instead of hard-coding :.

🎉 This issue has been resolved in version 2.0.16 🎉

The release is available on:

Your semantic-release bot 📦🚀

I have the same problem. Trying different ways to solve this issue i found a solution creating a new file in the root directory for the config of nodemon. Now i can be able to run my project without any problem The config file called nodemon.json have this inside:

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/", "build/"],
  "watch": ["/index.ts"],
  "execMap": {
    "ts": "node -r ts-node/register"
  },
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js,json,ts"
}

And in my package.json, my dev script call the config file for nodemon like this: "dev": "nodemon --config nodemon.json index.ts", For the moment, that works for me, but i will try to find a different way to implement nodemon, without having the necessity of create a config file to call it.

@abonvicini It’s working because you are not using ts-node but the ts-node/register register with node.

So you can have the same approach without a configuration file:

nodemon --watch index.ts --ext ts,json --exec node -r ts-node/register 

In my case I have:

nodemon --watch src --ext ts,json --exec node -r ts-node/register src/main.ts

This works for me. Using the last line and replacing ‘src/main.ts’ with my targeted file works for now.

Same issue here.

Package.json:

"scripts": {
    "dev": "nodemon main.ts"
},
"devDependencies": {
    "nodemon": "^2.0.14",
    "ts-node": "^10.4.0",
    "typescript": "^4.4.4"
}

Calling Nodemon:

$ npm run dev

> personalamt-server@0.1.0 dev
> nodemon main.ts

[nodemon] 2.0.14
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node main.ts`
'ts-node' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...

Calling ts-node:

$ npx ts-node main.ts
Hello World

I have the same problem. Trying different ways to solve this issue i found a solution creating a new file in the root directory for the config of nodemon. Now i can be able to run my project without any problem

The config file called nodemon.json have this inside:

{
  "restartable": "rs",
  "ignore": [".git", "node_modules/", "build/"],
  "watch": ["/index.ts"],
  "execMap": {
    "ts": "node -r ts-node/register"
  },
  "env": {
    "NODE_ENV": "development"
  },
  "ext": "js,json,ts"
}

And in my package.json, my dev script call the config file for nodemon like this: "dev": "nodemon --config nodemon.json index.ts",

For the moment, that works for me, but i will try to find a different way to implement nodemon, without having the necessity of create a config file to call it.

@abonvicini It’s working because you are not using ts-node but the ts-node/register register with node.

So you can have the same approach without a configuration file:

nodemon --watch index.ts --ext ts,json --exec node -r ts-node/register 

In my case I have:

nodemon --watch src --ext ts,json --exec node -r ts-node/register src/main.ts

Another workaround is to target directly the ts-node executable with node in nodemon.json :

{
    "watch": "src/**/*.ts",
    "execMap": {
        "ts": "node ./node_modules/ts-node/dist/bin.js"
    }
}

Or you can target :

  • bash file ("ts": "./node_modules/.bin/ts-node")
  • cmd file ("ts": "./node_modules/.bin/ts-node.cmd")
  • powershell file ("ts": "./node_modules/.bin/ts-node.ps1") Which all does more or less the same thing, except it’s platform specific

I see this same problem and will just chime in with my testing/experience if its helpful.

I can confirm the same problem exists on Windows using npm version 7 and 8. I attempted it with a git-bash shell, PowerShell and a good ol’ cmd shell.

Reverting back to npm 6 does resolve the issue for me. Meaning, nodemon finds ts-node that is installed locally and executes it.

Also, I sometimes use WSL. I tested this with WSL2 running Ubuntu 20.04. Using the latest npm (8.1.3) nodemon works as expected with ts-node.

So it appears to me to be an issue with npm >= 7 on Windows. I wonder if its a known breaking change or a bug. I didn’t research that.

(Note: I do a clean install of the node_modules between each of these tests)