You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS文件在VS Code终端可运行,Windows命令提示符运行失败求解决

Windows命令提示符运行Node.js文件的正确方法

问题背景

我的index.js文件内容为console.log('hello World');,在VS Code的PowerShell终端中执行node index.js可以正常输出hello World,但在Windows命令提示符(CMD)中运行时出现错误。

VS Code终端运行示例:

PS C:\js-basics> node index.js
hello World
PS C:\js-basics>

命令提示符运行报错:

C:\Users\godbe>PS C:\js-basics> node index.js
'PS' is not recognized as an internal or external command,
operable program or batch file.

我尝试了多种错误写法:

C:\Users\godbe>C:\js-basics node <index>.js
The system cannot find the file specified.

C:\Users\godbe>C:\js-basics $node index.js
'C:\js-basics' is not recognized as an internal or external command,
operable program or batch file.

正确操作方法

方法1:切换到文件所在目录后运行

  1. 在CMD中用cd命令进入index.js所在的C:\js-basics目录:
    C:\Users\godbe>cd C:\js-basics
    
    执行后提示符会变为:
    C:\js-basics>
    
  2. 直接输入Node.js运行命令:
    C:\js-basics>node index.js
    
    此时就能正常输出:
    hello World
    

方法2:不切换目录,直接指定文件完整路径

如果不想切换目录,可直接在命令里写文件的绝对路径:

C:\Users\godbe>node C:\js-basics\index.js

同样会输出hello World。


内容的提问来源于stack exchange,提问作者Jesse Godbey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 15:05:56