'ts-node'不被识别为内部或外部命令,可操作程序或批处理文件

时间:2021-10-02 21:22:45

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.

我在我的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"
}

}

3 个解决方案

#1


13  

You need to install ts-node as global

npm install -g ts-node

More information

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

#2


10  

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

我刚刚遇到了类似的问题:在Mac OS上--exec ts-node工作,在Windows上却没有。

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

我的解决方法是创建一个nodemon.json,如下所示:

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

and change the package.json scripts section to

并将package.json脚本部分更改为

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

#3


8  

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

我不建议在您自己的模块中依赖全局安装的ts节点,因为这里的一些答案建议。

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.

如果你这样做,那么安装你的模块的任何人都需要全局安装ts-node(只是通常的npm安装是不够的)然后如果两个模块需要像全局安装的ts-node那样你会遇到问题但是有不同的版本等

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

为避免这种情况,应在pa​​ckage.json中定义所有依赖项,并在node_modules中本地安装。

There is a little-known command npx that 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-node and typescript:

例如,看看我安装(本地)ts-node和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:

然后我尝试运行ts节点:

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.

无论如何,我不需要在全球范围内安装任何东西。

#1


13  

You need to install ts-node as global

npm install -g ts-node

More information

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

#2


10  

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

我刚刚遇到了类似的问题:在Mac OS上--exec ts-node工作,在Windows上却没有。

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

我的解决方法是创建一个nodemon.json,如下所示:

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

and change the package.json scripts section to

并将package.json脚本部分更改为

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

#3


8  

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

我不建议在您自己的模块中依赖全局安装的ts节点,因为这里的一些答案建议。

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.

如果你这样做,那么安装你的模块的任何人都需要全局安装ts-node(只是通常的npm安装是不够的)然后如果两个模块需要像全局安装的ts-node那样你会遇到问题但是有不同的版本等

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

为避免这种情况,应在pa​​ckage.json中定义所有依赖项,并在node_modules中本地安装。

There is a little-known command npx that 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-node and typescript:

例如,看看我安装(本地)ts-node和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:

然后我尝试运行ts节点:

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.

无论如何,我不需要在全球范围内安装任何东西。