如何调试NodeJS

查看全文:http://ramonblog.cloudfoundry.com/blog/5002d26f4e1ef7b729000002


nodejs系列提纲

我终于要不能忍受优快云这个垃圾网站的blog了,于是乎我决定借着学习nodejs的机会自己搭建一个blog,并且将其部署到cloudfoundry上。于是乎我将陆续完成下面的系列文章。所以文章里面的代码都是这个blog的实现。

  1. Why NodeJS
  2. nodejs的架构
  3. nodejs的hello world
  4. nodejs类库
  5. nodejs的plugin,如何实现一个plugin
  6. nodejs的web framework Expression
  7. nodejs与mongoDB
  8. nodejs与cloudfoundry
  9. nodejs如何调试(Node Inspector)


debug nodejs

Sure it's important to debug for any programming language, and it should be learnt at first time to use a new language. I forgot why I put this debug blog at the end of the serials blogs. No matter what's the reason, now we need to introduce how to debug nodejs. This article has two parts, one is the native debugger for nodejs, and another one is Node Inspector. In fact there is the third way to debug nodejs, based on Eclipse, which is not introduced here, but you can find more introduction from here: Using Eclipse as Node Applications Debugger.

微笑NodeJS Built-in debugger

  1. Prepare to debug Have below simple code as our sample, name it as 'debug.js'.

    var count = 10;
    var sum = 0;
    debugger;
    for (var i = 1; i < count; i++) {
        sum += i;
    }
    console.log(sum);
    

    Note we have debugger; at line 3, which means we put a breakpoint here. Actually it's the same with the normal Javascript running in browser Chrome, in which put a debugger; statement. The difference it we add this statement into js code running in browser to force the execution to pause at load time. But in to debug nodejs, it's not necessary, because in debug mode the execution is always paused at first line.

    。。。。。。


Node Inspector

Finally we finish the basic commands of built-in debugger. Now we introduce a debug tool with UI, Chrome. You know nodejs is built on V8 js engine, which is the engine for Chrome browser, therefore it's a good idea to use Chrome as debug tool.

  1. Setup debug

    To use node-inspector we need change our sample code as below, which is from node-inspector test code, name it as 'debug.js' too.

    var http = require('http');
    
    
    var x = 0;
    http.createServer(function (req, res) {
      x += 1;
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World ' + x);
    }).listen(8124);
    console.log('Server running at http://127.0.0.1:8124/');
    

    Node Inspector only supports nodejs HttpServer, therefore we cannot debug previous sample code in before section, If we want to debug that code, we need wrap it into a http server like above sample code.

    。。。。。。

查看全文:http://ramonblog.cloudfoundry.com/blog/5002d26f4e1ef7b729000002


### 使用 nvm 调整 Node.js 版本 #### 安装 NVM 为了能够使用 `nvm` 来管理 Node.js 的版本,首先需要安装 `nvm`。对于 macOS 用户来说,可以通过 Homebrew 或者直接从 GitHub 下载并按照说明进行安装[^2]。 #### 列出可用的 Node.js 版本 一旦 `nvm` 已经成功安装,在终端输入命令来查看可以安装的所有 Node.js 版本列表: ```bash nvm ls-remote ``` 这会显示所有可安装的 Node.js 发布版本号[^4]。 #### 安装特定版本的 Node.js 如果想要安装某个具体的 Node.js 版本,比如 v14.17.0,则可以在终端执行如下命令: ```bash nvm install 14.17.0 ``` 这条指令将会下载指定版本并且自动设置为当前使用的版本。 #### 查看已安装的本地 Node.js 版本 要了解目前机器上已经安装了哪些 Node.js 版本,可以运行下面这个命令: ```bash nvm ls ``` 它将展示所有通过 `nvm` 管理过的本地 Node.js 版本及其状态(即哪个是默认版本)[^3]。 #### 设置默认使用的 Node.js 版本 为了让系统每次启动时都使用某一个特定版本作为默认版本,可以用以下命令设定: ```bash nvm alias default <version> ``` 这里的 `<version>` 应替换为你希望设为默认的具体版本号,例如 `v14.17.0`。 #### 切换正在使用的 Node.js 版本 当有多个 Node.js 版本被安装后,随时都可以切换到另一个版本来进行开发测试等工作。只需要简单地键入: ```bash nvm use <version> ``` 同样地,这里也需要把 `<version>` 替换成目标版本号码[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值