ts-node/ts_node命令未被识别问题求助(已尝试常见方案)
问题描述
运行ts_node命令时触发以下错误:
ts_node : The term 'ts_node' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
- ts_node
+ CategoryInfo : ObjectNotFound: (ts_node:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
已按以下方式完成ts-node的本地及全局安装:
# 本地项目安装 npm install -D typescript npm install -D ts-node # 全局安装 npm install -g typescript npm install -g ts-node
环境配置:Node.js 18、Windows 11、npm 10.1.0、TypeScript 5.2.2
解决方案
- 修正命令拼写:ts-node的正确调用命令是
ts-node(连字符分隔),而非ts_node(下划线),先确认是否因拼写错误导致问题。 - 全局安装后校验环境变量:
- 在PowerShell中执行
npm config get prefix,获取npm全局包的安装根路径。 - 检查该路径下的
node_modules/.bin目录是否已加入系统环境变量Path。 - 若未添加,手动配置:右键「此电脑」→「属性」→「高级系统设置」→「环境变量」,在系统变量的
Path列表中新增上述路径,之后重启PowerShell生效。
- 在PowerShell中执行
- 本地安装的正确运行方式:
本地安装的ts-node无法直接全局调用,可通过两种方式运行:- 使用npx调用:
npx ts-node your-file.ts - 在项目
package.json中添加脚本:
执行"scripts": { "dev": "ts-node src/index.ts" }npm run dev即可运行指定文件。
- 使用npx调用:
- 修复全局包安装异常:
若全局安装存在权限或缓存问题,执行以下命令重新安装:npm uninstall -g ts-node typescript npm install -g ts-node typescript --force - 清理npm缓存后重装:
尝试清理缓存后重新安装,排除缓存损坏导致的问题:npm cache clean --force npm uninstall -g ts-node typescript npm install -g ts-node typescript
内容的提问来源于stack exchange,提问作者k.k
相关产品推荐
相关产品推荐

