'ts-node' is not recognized as an internal or external command, operable program or batch file

I'm getting error in my Vs Code terminal and command prompt that 'ts-node' is not recognized as an internal or external command, operable program or batch file. while i'm trying the start command in the terminal npm run dev and i have added my package.json file also.

{
"name": "tsnode",
"version": "1.0.0",
"description": "ts-node experiment.",
"scripts": {
    "dev": "nodemon --exec 'ts-node --cache-directory .tscache' ./server.ts",
    "start": "ts-node --fast ./server.ts"
},
"author": "Mugesh",
"license": "ISC",
"dependencies": {
    "@types/body-parser": "^1.16.3",
    "@types/chalk": "^0.4.31",
    "@types/express": "^4.0.35",
    "@types/node": "^7.0.18",
    "body-parser": "^1.17.1",
    "chalk": "^1.1.3",
    "express": "^4.15.2",
    "nodemon": "^1.11.0",
    "ts-node": "^3.0.4",
    "typescript": "^2.3.4"
}

}


Solution 1:

You need to install ts-node as global

npm install -g ts-node

More information

https://github.com/TypeStrong/ts-node

Solution 2:

I wouldn't recommend relying on globally installed ts-node in your own module as some of the answers here suggest.

If you do that then anyone who installs your module would need to install ts-node globally as well (just a usual npm install would not be enough) and then you will have a problem if two modules need things like ts-node globally installed but with different versions etc.

To avoid that, all your dependencies should be defined in your package.json and installed locally in node_modules.

There is a little-known command npx that is used to run binaries from modules that are installed locally in node_modules.

For example, see what happens when I install (locally) ts-node and typescript:

rsp@mn-r:~/node/test/ts-test-1$ npm i ts-node typescript
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ [email protected]
+ [email protected]
added 19 packages from 44 contributors in 2.157s
[+] no known vulnerabilities found [19 packages audited]

and then I try to run ts-node:

rsp@mn-r:~/node/test/ts-test-1$ ts-node -v
-bash: /Users/rsp/opt/node/bin/ts-node: No such file or directory

I can run it with npx:

127!rsp@mn-r:~/node/test/ts-test-1$ npx ts-node -v
ts-node v6.0.3
node v10.1.0
typescript v2.8.3

or I could give the path explicitly:

rsp@mn-r:~/node/test/ts-test-1$ ./node_modules/.bin/ts-node -v
ts-node v6.0.3
node v10.1.0
typescript v2.8.3

In any case, I don't need to install anything globally.

Solution 3:

I just encountered a similar issue: on Mac OS --exec ts-node works, on Windows it doesn't.

My workaround is to create a nodemon.json like this:

{
  "watch": "src/**/*.ts",
  "execMap": {
    "ts": "ts-node"
  }
}

and change the package.json scripts section to

"scripts": {
  "start": "nodemon src/index.ts"
},

Solution 4:

I ran into the same problem and found that it works by using double quotes instead of single.

"dev": "nodemon --exec \"ts-node\" --cache-directory .tscache ./server.ts"

P.S. This is 1 year after the problem. Not sure if package versions are a factor. Will confirm if needed.