Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Last active October 17, 2018 00:20
Show Gist options
  • Save tmpvar/8746416 to your computer and use it in GitHub Desktop.
Save tmpvar/8746416 to your computer and use it in GitHub Desktop.
using modules in your new nodejs module

Managing dependencies

Ok, so you've built your first module, but now you want to make it use one of the many libraries available via the npm registry.

finding modules

There are a few ways to find a module. You can use http://npmjs.org or http://npmsearch.com to find modules that may fit what you need.

some advice for choosing modules

Modules should have tests, and they should try to only export a single function

You'll develop more criteria as you start digging through more modules, don't be afraid to read their source!

installing modules

So you've found a module that you want to install, we'll use the request module to demonstrate how to make it a dependency of your-first-node-module

cd your-first-node-module
npm install --save request

Ok, that should have dumped a bunch of text to your terminal. NPM does a bunch of work to make sure it gets all of the dependencies (recursively!) of the module you are installing

the --save flag will update your package.json with the version of request installed

$ cat package.json
{
  "name": "your-first-node-module",
  "version": "1.0.0",
  "description": "very first module",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Elijah Insua <tmpvar@gmail.com> (http://tmpvar.com)",
  "license": "MIT",
  "dependencies": {
    "request": "~2.33.0"
  }
}

what does my dependency tree look like?

$ npm list
your-first-node-module@1.0.0 /Users/tmpvar/work/tmp/your-first-node-module
└─┬ request@2.33.0
  ├── aws-sign2@0.5.0
  ├── forever-agent@0.5.0
  ├─┬ form-data@0.1.2
  │ ├── async@0.2.10
  │ └─┬ combined-stream@0.0.4
  │   └── delayed-stream@0.0.5
  ├─┬ hawk@1.0.0
  │ ├── boom@0.4.2
  │ ├── cryptiles@0.2.2
  │ ├── hoek@0.9.1
  │ └── sntp@0.2.4
  ├─┬ http-signature@0.10.0
  │ ├── asn1@0.1.11
  │ ├── assert-plus@0.1.2
  │ └── ctype@0.5.2
  ├── json-stringify-safe@5.0.0
  ├── mime@1.2.11
  ├── node-uuid@1.4.1
  ├── oauth-sign@0.3.0
  ├── qs@0.6.6
  ├─┬ tough-cookie@0.12.1
  │ └── punycode@1.2.3
  └── tunnel-agent@0.3.0

Ok, thats great.. how do I use request?

We'll demonstrate that by using it in the repl

$ cd your-first-node-module
$ node
> var request = require('request')
undefined
> request.get('http://google.com/', function(error, response, body) { console.log(body); })
... whole bunch of data ...
.. pause ..
.. a bunch of html ..

And that's how you use a node module installed from npm

installing development time modules

if you need modules to support your tests or local development you can npm install --save-dev and they will be placed in your devDependencies section of your project's package.json.

note: these modules will only be installed when you npm install from inside of your project. Users of your project will not need to install the development dependencies and npm will ignore them when installing your module as a dependency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment