ts-node直接运行typescript文件 报错与解决

ts-node直接运行typescript文件

在vue项目中写ts文件,想在node环境运行调试代码。如何执行ts文件?

直接执行node index.ts 回报错。

解决如下:

//全局安装typescript和ts-node
npm install -g typescript
//npm install -g typescript-node 由于typescript-node不支持更高版本的ts
npm install -g ts-node //typescript@>=2.7

安装完成后就可以不用手动去编译成js文件,可以直接运行ts文件。

执行命令ts-node **.ts**

控制台报错:

zsh: command not found: typescript;

执行 npx ts-node index.ts ,再报错:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

修改package.json,或者tscongfig文件:

"module": "commonjs"

再次执行npx ts-node index.ts 。运行成功。

参考:https://bobbyhadz.com/blog/npm-command-not-found-ts-node

你可能感兴趣的:(ts-node直接运行typescript文件 报错与解决)