简体   繁体   English

Node.js:“访问被拒绝”

[英]Node.js: "Access is denied"

I'm trying to make a simple node.js app in VS Code.我正在尝试在 VS Code 中制作一个简单的 node.js 应用程序。 All im trying to do is:我想做的就是:

const name = "Tom";
console.log("Hello " + name);

When I run the code, I get the error: Program 'node.exe' failed to run: Access is denied .当我运行代码时,出现错误: Program 'node.exe' failed to run: Access is denied When I try to check Node is installed with node -v in windows cmd, it also says "Access is denied".当我尝试在 windows cmd 中使用node -v检查 Node 时,它还显示“访问被拒绝”。 How do I fix this issue?我该如何解决这个问题?

You don't have enough permission to execute node commands in CMD, to solve this你没有足够的权限在CMD执行node命令,解决这个问题

Run CMD as an Admin以管理员身份运行CMD

  1. Type cmd on your start menu在开始菜单中输入 cmd

  2. Right click on the "Command Prompt" menu item and select "Open file Location"右键单击“命令提示符”菜单项和 select“打开文件位置”

  3. Right click on the file and select properties.右键单击文件和 select 属性。

  4. Go to "Advanced" and select "Run as Administrator" and Ok. Go 到“高级”和 select“以管理员身份运行”,然后确定。

在此处输入图像描述

If this doesn't solve your problem -如果这不能解决您的问题 -

  1. Go to the "Security" tab. Go 到“安全”选项卡。

  2. Click "Edit"点击“编辑”

  3. Add "Everyone" and Ok添加“所有人”并确定

在此处输入图像描述

The fastest way that worked for me (Windows 10) was the following:对我 (Windows 10) 最有效的方法如下:

  1. Open "nodejs" folder location打开“nodejs”文件夹位置
  2. Right-click + R右键单击 + R
  3. Open "Security tab" + "Advanced"打开“安全选项卡”+“高级”
  4. Change Owner to "Administrator".将所有者更改为“管理员”。
  5. Add permissions for Administrator "Full control"添加管理员权限“完全控制”
  6. Tick "Replace all child objects permission entries..."勾选“替换所有子对象权限条目...”
  7. "Ok". “行”。 Save救球

✅ Done ✅ 完成

use command prompt instead of powershell in windows 10在 windows 10 中使用命令提示符代替 powershell

在此处输入图像描述

In my case I was using NVM and in admin mode CMD when I selected version nvm list and nvm use version_goes_here and did npm i and it worked like a charm I hope it helped you or anyone reading this.在我的例子中,当我选择版本nvm listnvm use version_goes_herenpm i时,我使用 NVM 并处于管理模式 CMD,它就像一个魅力,我希望它能帮助你或任何阅读本文的人。 :) :)

Add " cd " before your directory name in cmd.在 cmd 目录名前加上“ cd ”。

Ex : cd C:\Users..\js folder name( not the filename )例如cd C:\Users..\js文件夹名称(不是文件名

and then type " dir " in cmd.然后在 cmd 中输入“ dir ”。

Ex : dir例如目录

you will get a list of files present in that folder,And then just enter " node.jsfilename "您将获得该文件夹中存在的文件列表,然后只需输入“ node.jsfilename

[Ex][2] : node jsfilename [Ex][2] :节点jsfilename

now you will have the desired output in cmd.现在您将在 cmd 中拥有所需的 output。

I think Access Denied happened because your root directory was different than the file's root directory that's why we used "cd" change directory, and to view the files present in the current directory we used "dir".我认为访问被拒绝的发生是因为您的根目录与文件的根目录不同,这就是我们使用“cd”更改目录并查看当前目录中存在的文件的原因我们使用“dir”。 Then we opened the js file with node "node.jsfilename".I hope You Got it.I believe you can see the image I have Attached below.然后我们打开带有节点“node.jsfilename”的 js 文件。我希望你明白了。我相信你可以看到我在下面附加的图像。

Refer To this image below:请参考下图:
https://i.stack.imgur.com/oibrF.jpg https://i.stack.imgur.com/oibrF.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM