Writing npm packages
If you create some code that would be useful to others, you can distribute it as an npm package. To demonstrate this, we'll implement some slightly more complex functionality.
Note
You can find the example code for this section at https://github.com/NodeJsForDevelopers/autotoc. Note that, unlike previous chapters, there is not one per commit per heading. The listings in the rest of this section match the final version of the code.
We're going to implement a tool for generating a table of contents (ToC) by crawling a website. To help with this, we'll make use of a few other npm packages:
- request provides an API for making HTTP requests, which is higher-level and much simpler to use than the build in the Node.js http module
- cheerio provides jQuery-like HTML traversal outside of the browser environment
- denodeify, mentioned in Chapter 8, Mastering Asynchronicity, allows us to use the request library with promises instead of callbacks
Tip
It's common for npm
...