Debugging in node.js
Using “V8 inspector” node.js now allows debugging codes outside their node process. Thus to debug your node.js code, just run the following command in your terminal.
node — inspect myapp.js
This will run your source code in debug mode.
By default it runs on port 9229. If the port is blocked, run it on some other port: eg.:
node — inspect=9000 myapp.js
To apply breakpoints in your code, open your Chrome browser.
The latest “Chrome” versions have rolled out an extremely useful built-in plugin called “node-inspector”. To use it just follow the given steps:
- In your chrome browser, open the dev-tools.
- Click on the green node logo:
- In the connection tab click on the “Add connection” button.
- Here add “localhost:<your_port_number>”
- Now in the “Sources” tab click on “Filesystem”. Click on the “+” symbol to add the folder where your file is situated. This is a one time process.
- Finally on the source code window, click on the line number to add the breakpoint.
About NPM
Npm stands for Node Package Manager. It is a command line tool to install 3rd party packages/dependencies for your node.js project. This is done through a file called “package.json”.
A package.json file contains the metadata about your project. npm command called “npm init” initializes this file. This is the first step that anyone does before developing an independent node module. It is made of directives and elements which handle your entire module.
During initialization of package.json it will ask a set of answers for some mandatory directives like, “name”(name of your module/project in lowercase) & “version”(project version separated by 3 dots). There also some optional directives like
- description: a short description about your projects objective
- dependencies & devDependencies: names of the 3rd party dependencies required by the project with their versioning details. Eg.: “package_1” : “~0.3.0”. Following types of versioning
“*”: picks any version from > 0.0.0
“x”: picks for major/minor version. Eg.: 1.x or 1.x.x
“~”: for ~0.2.0 = ≥0.2.0 to <0.3.0
“^”: for ^0.2.0 = ≥0.2.0 to <1.0.0
- repository: has two inner elements (type & url). Eg. type can be “git”, url will be your “<projects_repo_url>”
- scripts: has inner elements which contains npm as well as terminal commands. Very useful during developing and making builds of the project. You can have scripts to start your server, watch file changes, create builds etc.
Certain npm commands that can be useful are:
- “npm i mypackage” : installs 3rd party dependency.
- “npm i -g mypackage”: installs package globally, in the npm-cache.
- “npm i -D mypackage”: installs it as a devDependency.
- “npm uninstall mypackage”: uninstalls the dependency.
- “npm cache clean“: deletes packages from cache folder
- “npm run <script_element>”: run the element inside script directive in package.json.
Detailed info at: https://docs.npmjs.com/files/package.json
Other points:
- Popular IDE for Js: Visual Studio Code
- Read Node.js latest docs for more info.
- Node js best practices: https://github.com/goldbergyoni/nodebestpractices
- Important 3rd party modules: nodemon, ramda, lodash, moment, request/axios, express, koa
Other parts: