71

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"
}

}

Mugesh
  • 723
  • 1
  • 5
  • 4
  • It's really simple - if you installed it locally in your project then you need to access it trough symlinks in .bin of node_modules ie: ./node_modules/.bin/ts-node or install globally (the less robust solution, as versions will differ based on projects) – Damian Pieszczyński May 27 '21 at 22:19

17 Answers17

83

You need to install ts-node as global

npm install -g ts-node

More information

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

Sam Quinn
  • 1,366
  • 13
  • 10
  • you are correct, If i try for a particular file in my root directory(ie., ts-node app.ts) its working, but its not working in my package.json run-scripts(dev) – Mugesh Jun 26 '17 at 16:37
  • 14
    This is not a great answer because it relies on other devs to install the same module globally. See rsp's answer [below](https://stackoverflow.com/a/50454232/2500659) – Daryl Van Sittert Aug 01 '18 at 16:04
  • 2
    No need to install globally. – Alex Rindone Aug 11 '20 at 04:23
71

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 ts-test-1@0.0.0 No description
npm WARN ts-test-1@0.0.0 No repository field.

+ ts-node@6.0.3
+ typescript@2.8.3
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.

rsp
  • 91,898
  • 19
  • 176
  • 156
30

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"
},
Daniel
  • 2,131
  • 1
  • 21
  • 26
25

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.

Brian
  • 1,661
  • 14
  • 25
14

If you work under Windows you can't use single quote in the json file. That is why you have to replace all single quote symbols(') by the double quote symbols("). But between two double quotes(") you have to use escaped double quote(\"). For the current case you have to change the row in the file "package.json":

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

into the row:
"dev": "nodemon --exec \"ts-node --cache-directory .tscache\" ./server.ts",

RoutesMaps.com
  • 1,356
  • 1
  • 11
  • 17
8

For me deleting node_modules and installing it again using npm i was enough.

karoluS
  • 2,066
  • 1
  • 16
  • 31
6

I had the similar problem, but I have resolved by replacing

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

to

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

Just remove the single quote(') and install ts-node globally

Umashankar Saw
  • 961
  • 1
  • 8
  • 21
4

I fixed the issue by removing single quorts around ts-node. as per below "dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts"

updated as "dev": "nodemon --watch 'src/**/*.ts' --exec ts-node src/index.ts"

please note. my environment is windows 10 and npm version6.14.4

Dinesh
  • 65
  • 3
  • My original project was built on Ubuntu 18, and worked without issues. Moved to win10, had this error, and this worked for me. – Rocky Kev Aug 02 '20 at 19:10
3

I had a similar problem while using nodemon:

  • I had nodemon installed globally, AND ts-node only installed locally.

Solution:

  • I installed ts-node globally (still keeping the local dependency).
2

The only solution that worked for me:

"start": "nodemon --exec npx ts-node ./index.ts",
1

If your ts-node isn't working, as an alternative you can do the following:

1) Install nodemon locally --> npm i nodemon

2) In your package.json 'scripts' add the following:

"scripts": {
    "start": "nodemon index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

3) Now run npm start (this will automatically run node for you, but this WILL NOT COMPILE TS )

4) Open a new tab in the terminal/command line, cd the folder your working in and run tsc index.tsc --watch
This will compile your typescript. The only downside is you will just have to have both tabs open, one for running node automatically and the other for compiling automatically, but this works.

B. Go
  • 1,404
  • 4
  • 13
  • 21
Jackie Santana
  • 416
  • 4
  • 9
1

I was having the same issue on windows. I found the solution for my issue was resolved when I corrected some misplaced '

Originally:

"scripts": {
    "dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
    "build": "tsc",
    "start": "node dist/index.js"
}

Fixed:

"scripts": {
    "dev": "nodemon --watch 'src/**/*.ts' --exec \"ts-node\" src/index.ts",
    "build": "tsc",
    "start": "node dist/index.js"
}

The difference in case it isn't clear is that I no longer wrap ts-node in '

* EDIT * I changed this based on the answer from @RoutesMaps.com above. This solved my problem as well as removing the ' but @RoutesMaps.com actually explains the issue resolution

CavemanAGz
  • 43
  • 1
  • 9
0

If you are using a mac these are the steps I came up with in order to fix this in the terminal.

  1. Install globaly and use the returned file path with the symlink ‘ts-node’ and move this file into /usr/local/bin
  2. Install locally without saving to package.json
  3. copy folder in /node_modules into /usr/local/lib/node_modules/
  4. Make sure the file is executable by opening /ts-node/dist and using the command chmod +x bin.js
  5. run npm i in ts-node folder
  6. Make sure that dist folder still exsists, if not copy it back over.
  7. Test running ts-node in terminal, if it does not work it will return an error of which module needs to be moved over to ../
  8. After ts-node runs be sure to delete the folder /usr/local/lib/node_modules/ts-node/node_modules
Coded Container
  • 743
  • 2
  • 10
  • 30
0

I was having the same issue. I found the solution for my issue was resolved when i do simply run this command first "npm run build" and than try it nodemon and also add in package.json

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"dev": "ts-node ./lib/server.ts",
"start": "nodemon ./dist/server.js",
"prod": "npm run build && npm run start"}
Zubair Saif
  • 678
  • 13
  • 23
0

Nodemon is for watching and rerunning node processes when files change. The local ts-node installed in the node_modules directory is not recognized in the scope of the --exec argument. To get around this, some people have recommended installing ts-node globally. As a user pointed out, that's not a good solution because it relies on packages external to your project and makes the ts-node in our node_modules pointless.

To fix your solution, prefix ts-node with the npx helper, which will use your local node_module executables.

package.json, inside the scripts block:

"start": "nodemon --watch './src/index.ts' --exec 'npx ts-node src/index.ts'"

An alternative approach could be to use the typescript watcher with the existing node command and the concurrently package.

"start": "concurrently \"tsc --watch\" \"node ./dist/index.js\""

Same principle. One package watches for changes (nodemon & tsc) and restarts the second process (the node/ts-node server).

Daniel Foust
  • 584
  • 1
  • 5
  • 24
0

If you are using code-runner in vs-code then edit setting.json file

"typescript": "tsc $fileName && node $fileNameWithoutExt.js "

-1

I removed it from dev dependencies and added it to dependencies. That solved the problem for my case.

Esqarrouth
  • 35,175
  • 17
  • 147
  • 154