Node.JS - 如何从任何路径运行节点命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8278143/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:48:40  来源:igfitidea点击:

Node.JS - How to Run Node Command from any path?

node.jsenvironment-variables

提问by nfa379

I have installed the latest node.js from here. I see the %Path% variables have been set for Node.js. But, When i run node from C:\ , i get a "Node is not an recognized comman". But node command is working fine from "C:\Program Files (x86)\nodejs" path. Can i not run Node commands from C:\ or Say from an other path if i set the Env variables right? Please help. I am running a Win7 X64.

我从安装了最新的node.js这里。我看到已为 Node.js 设置了 %Path% 变量。但是,当我从 C:\ 运行节点时,我得到一个“节点不是公认的命令”。但是 node 命令在“C:\Program Files (x86)\nodejs”路径下工作正常。如果我正确设置了 Env 变量,我不能从 C:\ 或从其他路径运行 Node 命令吗?请帮忙。我正在运行 Win7 X64。

Thank You, Faraaz

谢谢你,法拉兹

采纳答案by respectTheCode

What happens if you run node --version? I have never tried node.js from windows but if that doesn't work there is something wrong with your paths.

如果你跑会发生什么node --version?我从未尝试过 Windows 中的 node.js,但如果这不起作用,则说明您的路径有问题。

What do you get when you run echo %PATH%?

当你跑步时你会得到什么echo %PATH%

回答by Victor Priceputu

I had the exact same problem, but in my PATH variable I had: C:\Program Files\nodejs\. After changing it to C:\Program Files\nodejsI could access it from anywhere.

我遇到了完全相同的问题,但在我的 PATH 变量中我有:C:\Program Files\nodejs\. 将其更改为C:\Program Files\nodejs我可以从任何地方访问它。

回答by pomeh

You should check if the path C:\Program Files (x86)\nodejsis present in your %PATH. If not, add it via the system properties (persistent change). In any case, restart your command prompt, it'll reload the %PATHvariable. If you're lucky, it should work now :)

您应该检查路径C:\Program Files (x86)\nodejs是否存在于您的%PATH. 如果没有,请通过系统属性添加它(持续更改)。无论如何,重新启动命令提示符,它会重新加载%PATH变量。如果你很幸运,它现在应该可以工作了:)

回答by mixture

Most likely it happens because there is no Node path in your PATH environment variable.

很可能是因为您的 PATH 环境变量中没有 Node 路径。

I see two ways to solve this

我看到有两种方法可以解决这个问题

  1. Just add Node path into the end of PATH.
  2. Reinstall Node as administrator
  1. 只需将节点路径添加到 PATH 的末尾。
  2. 以管理员身份重新安装节点

run 7-zip(or some file manager) with administrator privileges >> browse to node-installer.msi >> open it, install node >> enjoy

以管理员权限运行7-zip(或某些文件管理器)>> 浏览到 node-installer.msi >> 打开它,安装 node >> 享受

回答by snow6oy

My Sublime was working with Nodejs fine. I could press Alt-R and see the output from Node in Sublime. Then at some point I upgraded Sublime. I now have version 2.02 build 2221 And at some later point I tried Alt-R and got this infamous message

我的 Sublime 与 Nodejs 一起工作得很好。我可以按 Alt-R 并在 Sublime 中查看 Node 的输出。然后在某个时候我升级了 Sublime。我现在有版本 2.02 build 2221 后来我尝试了 Alt-R 并收到了这条臭名昭著的消息

'node' is not recognized as an internal or external command

“节点”未被识别为内部或外部命令

But it had worked, so what changed?

但它奏效了,那么什么改变了?

The steps I took to fix it were to install git bash and then

我修复它的步骤是安装 git bash 然后

$ git clone https://github.com/tanepiper/SublimeText-Nodejs.git
$ cd SublimeText-Nodejs/
$ git checkout 095ba03344

Back in Sublime I clicked Preferences > Browse Packages. This opened up Windows Explorer with the folder:

回到 Sublime,我点击Preferences > Browse Packages。这打开了带有文件夹的 Windows 资源管理器:

C:\Users\me\AppData\Roaming\Sublime Text 2\Packages

I replaced the contents of the Nodejs folder with the git version. Re-started Sublime and then Alt-R sprang back into life.

我用 git 版本替换了 Nodejs 文件夹的内容。重新启动 Sublime,然后 Alt-R 重新焕发生机。

Other things that turned out to be irrelevant were changing the file Nodejs.sublime-settingsand tampering with system environment variables. The only thing that worked was 095ba03344as described here https://github.com/tanepiper/SublimeText-Nodejs/pull/39

其他无关紧要的事情是更改文件Nodejs.sublime-settings和篡改系统环境变量。唯一有效的是095ba03344,如此处所述 https://github.com/tanepiper/SublimeText-Nodejs/pull/39