少一点心柿 發表於 2020-6-5 10:32:00

vscode 运行和调试 javascript 代码 (vscode run and debug javascript code)

<p>&nbsp;</p>
<p><span style="font-size: 16px">初次正式要写 javascript 相关的代码,想要用 vscode 直接编译 js 代码,但是发现没有那么简单,需要配置好 launch.json 文件,现已经在vscode上编译过去并且可以调试 javascript 代码,总结了两种方法,分享给大家.</span></p>
<p><strong><span style="font-size: 16px">方法一: 在 js 后缀文件中写 javascript 代码.</span></strong></p>
<p>&nbsp;</p>
<p><span style="color: rgba(128, 0, 128, 1)"><span style="background-color: rgba(255, 255, 255, 1)"><span style="font-size: 16px">1. 环境配置</span><span style="font-size: 16px">: </span></span></span></p>
<p><span style="font-size: 16px">(1). 需要安装 nodejs&nbsp; (在Bing搜索中输入 nodejs, 找到nodejs官网,然后找到适合你电脑配置的安装包进行下载安装,最后要输入 node -v 和 npm -v 检验是否安装成功)</span></p>
<p><span style="font-size: 16px">(2). (可选项,可以安装下看下结果)如果你想直接看结果,不想debug调试也可以安装 vscode 扩展包: Code Runner 运行 javascript 代码</span></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px; color: rgba(128, 0, 128, 1)">2. 新建一个 js 后缀的文件,如 hello_world.js ,输入以下内容:</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">var</span> a = 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">var</span> b = 2<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> console.log("hello world"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">4</span> console.log("a = "<span style="color: rgba(0, 0, 0, 1)">, a);
</span><span style="color: rgba(0, 128, 128, 1)">5</span> console.log("b = ", b);</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px; color: rgba(128, 0, 128, 1)">3. 运行程序的三种方法</span></p>
<p><span style="font-size: 16px">方法一: 如果你安装了 Code Runer,也就是做了环境配置的第二步了。那么就可以直接点击右键选择 Run Code 运行代码,就可以在 <span style="color: rgba(128, 0, 128, 1)">OUTPUT 窗口</span>上看到运行结果</span></p>
<p><span style="font-size: 16px">方法二:在 vscode 的 <span style="color: rgba(128, 0, 128, 1)">TERMINAL 终端</span>输入: node hello_world.js 也可以看到 运行结果</span></p>
<p><span style="font-size: 16px">方法三:如果你想要按下 F5 进行运行并且调试,那么就要配置好 launch.json 文件. 先点击 Run -&gt; Open Configurations, 输入以下内容</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Use IntelliSense to learn about possible attributes.</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Hover to view descriptions of existing attributes.</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>   "version": "0.2.0"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>   "configurations"<span style="color: rgba(0, 0, 0, 1)">: [{
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>         "name": "Launch"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>         "type": "node"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">10</span>         "request": "launch"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">11</span>         "program": "${workspaceRoot}/hello_world.js"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">      },
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">    ]
</span><span style="color: rgba(0, 128, 128, 1)">14</span> }</span></pre>
</div>
<p><span style="color: rgba(255, 0, 0, 1)"><strong><span style="font-size: 16px">注意这里的第 11 行的文件名称要改成你自己定义的文件名称,其中的&nbsp;workspaceRoot 表示当前文件路径.</span></strong></span></p>
<p><span style="font-size: 16px">再按下 F5 的时候,记得配置文件一定要选择名为 Launch (和上面的name同名) 的那个配置文件运行,配置了 launch.json 文件,你还可以在 js 文件中打上断点进行调试.如下图所示</span></p>
<p><span style="font-size: 16px"><img src="https://img2020.cnblogs.com/blog/1703588/202006/1703588-20200605100912147-1164197114.png" alt="" width="1142" height="752"></span></p>
<p><span style="font-size: 16px; color: rgba(255, 0, 0, 1)">(如果你debug成功请忽略这一段话)如果你第一种和第二种方法你都成功了(说明你的代码没有问题),但是使用第三种方法进行 debug 的时候一直不能显示 debug 效果,什么都没有显示和反应,那么可以试下在 setting.json 文件中加上以下语句应该就没问题了:&nbsp;</span><span style="color: rgba(255, 0, 0, 1); font-size: 16px">"debug.javascript.usePreview": false,&nbsp; &nbsp; 参考资料:https://github.com/microsoft/vscode-js-debug/issues/643</span></p>
<p>&nbsp;</p>
<p><strong><span style="font-size: 16px">方法二: 在 html 后缀文件中写 javascript 代码.</span></strong></p>
<p><span style="font-size: 16px">1. 环境配置:</span></p>
<p><span style="font-size: 16px">(1)&nbsp; 安装 chrome 浏览器(做前端开发这是通用浏览器)</span></p>
<p><span style="font-size: 16px">(2)&nbsp; 安装 vscode 扩展包:&nbsp; Debugger for chrome 和 open in browser</span></p>
<p><span style="font-size: 16px">(3)&nbsp; File -&gt; Preferences -&gt; Settings, 输入 breakpoints,找到 Debug: Allow Breakpoints Everywhere,勾上允许在任何文件设置断点(这样才可以在html文件中设置断点)</span></p>
<p><span style="font-size: 16px">2. 新建一个 html 后缀的文件,如 a.html ,输入以下内容:</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;!</span><span style="color: rgba(255, 0, 255, 1)">DOCTYPE html</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">head</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 255, 1)">function</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)"> myFunction()
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    console.log(</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">hello world</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    document.getElementById(</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">demo</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">).innerHTML</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">=</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">My First JavaScript Function</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">    alert(</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">this is a place where can write code.</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">head</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">14</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">body</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">h1</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>My Web Page<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">h1</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">p </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="demo"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>A Paragraph<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">p</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">20</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)">type</span><span style="color: rgba(0, 0, 255, 1)">="button"</span><span style="color: rgba(255, 0, 0, 1)"> onclick</span><span style="color: rgba(0, 0, 255, 1)">="myFunction()"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Try it<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">body</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></span></pre>
</div>
<p><span style="font-size: 16px">3. 运行程序</span></p>
<p><span style="font-size: 16px">(1) 按下 F5 运行并且调试代码,这里主要涉及到 launch.json 文件的配置,先点击 Run -&gt; Open Configurations, 输入以下内容</span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">    // Use IntelliSense to learn about possible attributes.
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)">    // Hover to view descriptions of existing attributes.
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)">    "version": "0.2.0",
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)">    "configurations": [
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)">            "type": "chrome",
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)">            "request": "launch",
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)">            "name": "Launch Chrome against localhost",
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">            "url": "http://localhost:8080",
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">            "webRoot": "${workspaceFolder}"
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">      },
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 0, 1)">            "type": "chrome",
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">            "request": "launch",
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">            "name": "使用本地chrome调试",
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)">            "file": "${file}",
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 0, 1)">            "port":8000,
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 0, 1)">      }
</span><span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(0, 0, 0, 1)">    ]
</span><span style="color: rgba(0, 128, 128, 1)">22</span> }</span></pre>
</div>
<p><span style="font-size: 16px">然后在 script 的代码部分打上断点,按下 F5 , 点击 Try it 按钮,你可以看到中间结果了,如下图所示</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1703588/202006/1703588-20200605103104080-364987255.png" alt="" width="1271" height="689"></p>
<p>&nbsp;</p>
<p><span style="font-size: 16px">(2) 鼠标右键点击 Open in Other Browsers, 选择其中 一个浏览器就可以看到结果,再点击按钮出现的网页中的 Try it 按键,就可以调用 script 中 js 的代码的结果. 这里,你也可以在vscode中设置你的默认浏览器,那么你就可以选择Open in Default Browers, 在默认的浏览器中打开, 或者按下快捷键 Alt + B 查看结果.&nbsp; (这种方法不能调试,并且这种方法只能在配置好launch.json后再按下F5之后才可以使用)</span></p>
<p><span style="font-size: 16px">(备注: vscode 默认浏览器的设置, File -&gt; Preferences -&gt; Settings, 输入 default browser , 找到 Open-in-browser : Default , 我这里是输入了 : Google Chrome )</span></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/ttweixiao-IT-program/p/13048275.html
頁: [1]
查看完整版本: vscode 运行和调试 javascript 代码 (vscode run and debug javascript code)