基本 node.js 项目的“属性‘程序’不存在”

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

"Attribute 'program' does not exist" for basic node.js project

node.jsvisual-studio-code

提问by Valeriy

I created simple node.js application (source code from here https://azure.microsoft.com/en-us/blog/visual-studio-code-and-azure-app-service-a-perfect-fit/)

我创建了简单的 node.js 应用程序(来自这里的源代码https://azure.microsoft.com/en-us/blog/visual-studio-code-and-azure-app-service-a-perfect-fit/

var http = require('http');
http.createServer(function (req, res) {
    console.log('Got request for ' + req.url);
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('<h1>Hello Code and Azure Web Apps!</h1>');
}).listen(process.env.PORT);

And clicked VSCode generated launch.json:

然后点击 VSCode 生成的 launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/app.js",
            "stopOnEntry": false,
            "args": [],
            "cwd": "${workspaceRoot}",
            "preLaunchTask": null,
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": false,
            "outDir": null
        },
        {
            "name": "Attach",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "address": "localhost",
            "restart": false,
            "sourceMaps": false,
            "outDir": null,
            "localRoot": "${workspaceRoot}",
            "remoteRoot": null
        }
    ]
}

And still when launched I see:

仍然在启动时我看到:

Attribute 'program' does not exist.

属性“程序”不存在。

Can anybody help what's wrong?

有人可以帮忙吗?

采纳答案by mdickin

I believe that you need ${workspaceRoot}/server.js, not ${workspaceRoot}/app.jsfor program. The code you're using doesn't have an app.js, that's what that (poorly worded) error is telling you.

我相信你需要${workspaceRoot}/server.js,而不是${workspaceRoot}/app.js为了program。您使用的代码没有 app.js,这就是(措辞不佳)错误告诉您的。

回答by Colin

I also encountered this issue because of where VS Code put the .vscode directory containing the launch.json file. It put it up one directory so I had to add the directory to the path as defined in the launch.json file:

我也遇到了这个问题,因为 VS Code 将包含 launch.json 文件的 .vscode 目录放在哪里。它把它放在一个目录中,所以我必须将该目录添加到 launch.json 文件中定义的路径:

"program": "${workspaceRoot}/myDir/app.js",

"program": "${workspaceRoot}/myDir/app.js",

I hope this helps.

我希望这有帮助。

回答by ripper234

Another issue I ran into is a path was configured Using\\Backslashes\\Like\\Soand worked fine on Windows, but on Mac it gave the above error.

我遇到的另一个问题是路径已配置Using\\Backslashes\\Like\\So并且在 Windows 上运行良好,但在 Mac 上却出现了上述错误。

(Solution: changed to /)

(解决方法:改为/

回答by user3179473

The error is saying that the path to your code was wrong.

错误是说您的代码路径错误。

VSCode defines the parent directory of its configuration file ".vscode/launch.json" as "${workspaceRoot}" or "${workspaceFolder}".

VSCode 将其配置文件“.vscode/launch.json”的父目录定义为“${workspaceRoot}”或“${workspaceFolder}”。

So, for example, if you want to run file "myproject/subfolder/main.js", you should configure your "myproject/.vscode/launch.json" as follows: "program": "${workspaceRoot}/subfolder/main.js"

因此,例如,如果要运行文件“myproject/subfolder/main.js”,则应按如下方式配置“myproject/.vscode/launch.json”: "program": "${workspaceRoot}/subfolder/main.js"

Note that configuring "program": "${workspaceRoot}/myproject/subfolder/main.js" is a mistake and will cause error "Attribute 'program' does not exist".

请注意,配置 "program": "${workspaceRoot}/myproject/subfolder/main.js" 是一个错误,会导致错误“属性‘程序’不存在”。

回答by Awad Maharoof

I wasted a few hours today trying to figure this problem out. What worked for me was deleting the existing launch.json and running the application, which prompts you to select an enviroment, which in my case was Node. This created a new launch.json in which I updated the program path.

我今天浪费了几个小时试图解决这个问题。对我有用的是删除现有的 launch.json 并运行应用程序,这会提示您选择一个环境,在我的情况下是 Node。这创建了一个新的 launch.json,我在其中更新了程序路径。

回答by Alex Wachira

The error should ideally read 'file specified in program attribute does not exist' because that is what is happening. As of VSCode 1.30.2, it does show you the path along with the error.

理想情况下,错误应该是“程序属性中指定的文件不存在”,因为这就是正在发生的事情。从 VSCode 1.30.2 开始,它确实向您显示了路径和错误。

In my case I had "program": "${workspaceFolder}\\${file}"so the path was something like c:\dir\c:\dir\file.js

在我的情况下,我有 "program": "${workspaceFolder}\\${file}"所以路径是这样的c:\dir\c:\dir\file.js

I corrected this by removing ${workspaceFolder}since I wanted to be able to debug individual files.

我通过删除${workspaceFolder}来纠正这个问题,因为我希望能够调试单个文件。

回答by Orhan Celik

I had the same issue. In my case my launch.json had following line

我遇到过同样的问题。在我的情况下,我的 launch.json 有以下行

"program": "${workspaceFolder}\index.js"

My active code that I tried to debug was in app_v2.js , so I updated it to following, and then debug worked.

我尝试调试的活动代码在 app_v2.js 中,因此我将其更新为以下内容,然后调试工作。

"program": "${workspaceFolder}\app_v2.js"

回答by Mjyousse

I had the same question and took me couple of hours to figure it out. What I basically did was that I deleted the folder after ${workspaceFolder}

我有同样的问题,花了我几个小时才弄明白。我基本上做的是在之后删除了文件夹${workspaceFolder}

The format was ${workspaceFolder}/xxxx\\folder\\subfolder\\subfolderso by deleting what's after the "workspaceFolder" and starting my path from the double backward slash, it did fix it for me.

${workspaceFolder}/xxxx\\folder\\subfolder\\subfolder通过删除“workspaceFolder”之后的内容并从双反斜杠开始我的路径,格式是这样的,它确实为我修复了它。

回答by Avid Programmer

Firstly, read the official documentthis answers all question you would have about setting the right attributes for different scenarios using launch.json.

首先,阅读官方文档,它回答了您关于使用launch.json.

Now, to specifically answer this question, the ${workspaceFolder}is basically containing directory of the .vscodedirectory, which is your project root directory. So, when setting specific files as your debugging program, remember to map the path from the project root directory, or in other words the relative path of the file that is to be set as the debugging program. This can be easily obtained from the IDE (VS Code) by simply right-clicking the file and selecting the Copy Relative Pathoption. Then proceed to paste this next to the ${workspaceFolder} in the program attribute in your launch.jsonfile, like below, will fix the problem.

现在,具体回答这个问题,${workspaceFolder}基本上是包含目录的.vscode目录,也就是你的项目根目录。因此,在将特定文件设置为调试程序时,请记住从项目根目录映射路径,即要设置为调试程序的文件的相对路径。只需右键单击文件并选择该Copy Relative Path选项,即可从 IDE (VS Code) 轻松获得此信息。然后继续将其粘贴到launch.json文件中程序属性中的 ${workspaceFolder} 旁边,如下所示,将解决问题。

"program": "${workspaceFolder}/<relative_path>"

Replace relative path with your copied relative path as mentioned beforeNote that I am on a Mac platform. Please use platform appropriate path separators

用之前提到的复制的相对路径替换相对路径请注意,我在 Mac 平台上。请使用平台适当的路径分隔符

Alternatively, not specifically using a launch configuration makes sense if it's a not-for-production or a simple app that does not warrant a launch config file. However, if not, it is super useful when debugging in a Multi-target environment (server, client). In my opinion, using a compound launch configuration setup makes things a lot easier. Read this sectionof the official docs to learn how to set it up keeping in mind the relative paths of your server and client files.

或者,如果它不是用于生产的或不保证启动配置文件的简单应用程序,则不专门使用启动配置是有意义的。但是,如果没有,它在多目标环境(服务器、客户端)中调试时非常有用。在我看来,使用复合启动配置设置会使事情变得容易得多。阅读官方文档的这一部分以了解如何设置它并记住服务器和客户端文件的相对路径。

回答by Carlos Nantes

I had the same error, because I was passing the arguments inside "program" attribute like this:

我有同样的错误,因为我在“程序”属性中传递参数是这样的:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Build -B -p",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\app\build -B -p D:\apps\12"
        }
    ]
}

What solved for me was to pass the arguments inside "args" attribute, like this:

为我解决的是在“args”属性中传递参数,如下所示:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Build -B -p",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\app\build",
            "args":["-B", "-pD:\apps\12"]
        }
    ]
}

The O.S. was Windows 7.

操作系统是 Windows 7。