node.js 'ts-node' 不是内部或外部命令,也不是可运行的程序或批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44764004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'ts-node' is not recognized as an internal or external command, operable program or batch file
提问by Mugesh
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 devand i have added my package.json file also.
我的 Vs Code 终端和命令提示符出现错误,提示“ts-node”未被识别为内部或外部命令、可运行的程序或批处理文件。当我在终端npm run dev 中尝试启动命令时,我也添加了我的 package.json 文件。
{
"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"
}
}
}
回答by rsp
I wouldn't recommend relying on globally installed ts-nodein your own module as some of the answers here suggest.
我不建议依赖全局安装ts-node在您自己的模块中,因为这里的一些答案建议。
If you do that then anyone who installs your module would need to install ts-nodeglobally as well (just a usual npm installwould not be enough) and then you will have a problem if two modules need things like ts-nodeglobally installed but with different versions etc.
如果你这样做,那么安装你的模块的任何人都需要ts-node全局安装(只是通常npm install是不够的),然后如果两个模块需要ts-node全局安装但具有不同版本等,你就会遇到问题。
To avoid that, all your dependencies should be defined in your package.json and installed locally in node_modules.
为避免这种情况,您的所有依赖项都应在 package.json 中定义,并在本地安装在 node_modules 中。
There is a little-known command npxthat is used to run binaries from modules that are installed locally in node_modules.
有一个鲜为人知的命令npx用于从本地安装在 node_modules 中的模块运行二进制文件。
For example, see what happens when I install (locally) ts-nodeand typescript:
例如,看看当我安装(本地)ts-node和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:
然后我尝试运行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:
我可以运行它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.
无论如何,我不需要全局安装任何东西。
回答by Sam Quinn
You need to install ts-node as global
您需要将 ts-node 安装为全局
npm install -g ts-node
More information
更多信息
回答by Daniel
I just encountered a similar issue: on Mac OS --exec ts-nodeworks, on Windows it doesn't.
我刚刚遇到了一个类似的问题:在 Mac OS 上--exec ts-node有效,在 Windows 上却没有。
My workaround is to create a nodemon.jsonlike this:
我的解决方法是创建一个nodemon.json这样的:
{
"watch": "src/**/*.ts",
"execMap": {
"ts": "ts-node"
}
}
and change the package.jsonscripts section to
并将package.json脚本部分更改为
"scripts": {
"start": "nodemon src/index.ts"
},
回答by Brian
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.
PS这是问题发生后的 1 年。不确定软件包版本是否是一个因素。如果需要,将确认。
回答by karoluS
For me deleting node_modulesand installing it again using npm iwas enough.
对我来说,删除node_modules并再次安装它就npm i足够了。
回答by RoutesMaps.com
If you work under Windowsyou 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":
如果您在 Windows 下工作,则不能在 json 文件中使用单引号。这就是为什么你必须用双引号 (") 替换所有单引号 (')。但在两个双引号 (") 之间你必须使用转义双引号 (\")。对于当前情况,你必须更改文件“package.json”中的行:
"dev": "nodemon --exec 'ts-node --cache-directory .tscache' ./server.ts",
"dev": "nodemon --exec 'ts-node --cache-directory .tscache' ./server.ts",
into the row:
"dev": "nodemon --exec \"ts-node --cache-directory .tscache\" ./server.ts",
进入行:
"dev": "nodemon --exec \"ts-node --cache-directory .tscache\" ./server.ts",
回答by Umashankar Saw
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
只需删除单引号(')并全局安装 ts-node
回答by Tuhafeni Angula
I had a similar problem while using nodemon:
我在使用时遇到了类似的问题nodemon:
- I had
nodemoninstalled globally, ANDts-nodeonly installed locally.
- 我已经
nodemon全局安装,并且ts-node只安装在本地。
Solution:
解决方案:
- I installed
ts-nodeglobally (still keeping the local dependency).
- 我
ts-node全局安装(仍然保持本地依赖)。
回答by Hymanie Santana
If your ts-node isn't working, as an alternative you can do the following:
如果您的 ts-node 不工作,作为替代,您可以执行以下操作:
1) Install nodemon locally --> npm i nodemon
1) 在本地安装 nodemon --> npm i nodemon
2) In your package.json 'scripts' add the following:
2) 在你的 package.json 'scripts' 添加以下内容:
"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?)
3)现在运行npm start(这将自动为您运行节点,但这将?不?编译?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.
4)在终端/命令行中打开一个新选项卡, cd 您正在工作的文件夹并运行tsc index.tsc --watch
这将编译您的打字稿。唯一的缺点是您只需要打开两个选项卡,一个用于自动运行节点,另一个用于自动编译,但这有效。
回答by Esqarrouth
I removed it from dev dependencies and added it to dependencies. That solved the problem for my case.
我从开发依赖项中删除了它并将其添加到依赖项中。这解决了我的案子的问题。

