Skip to content

Instantly share code, notes, and snippets.

@NeoTech
Forked from tmpvar/managing-dependencies.md
Created February 1, 2014 01:09
Show Gist options
  • Save NeoTech/8746517 to your computer and use it in GitHub Desktop.
Save NeoTech/8746517 to your computer and use it in GitHub Desktop.

Revisions

  1. @tmpvar tmpvar revised this gist Feb 1, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion managing-dependencies.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ## Managing dependencies

    Ok, so you've built your first module, but now you want to make it use one of the many libraries availble via the npm registry.
    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
  2. @tmpvar tmpvar created this gist Feb 1, 2014.
    102 changes: 102 additions & 0 deletions managing-dependencies.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    ## Managing dependencies

    Ok, so you've built your first module, but now you want to make it use one of the many libraries availble 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`

    ```bash
    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 <[email protected]> (http://tmpvar.com)",
    "license": "MIT",
    "dependencies": {
    "request": "~2.33.0"
    }
    }
    ```

    #### what does my dependency tree look like?

    ```
    $ npm list
    [email protected] /Users/tmpvar/work/tmp/your-first-node-module
    └─┬ [email protected]
    ├── [email protected]
    ├── [email protected]
    ├─┬ [email protected]
    │ ├── [email protected]
    │ └─┬ [email protected]
    │ └── [email protected]
    ├─┬ [email protected]
    │ ├── [email protected]
    │ ├── [email protected]
    │ ├── [email protected]
    │ └── [email protected]
    ├─┬ [email protected]
    │ ├── [email protected]
    │ ├── [email protected]
    │ └── [email protected]
    ├── [email protected]
    ├── [email protected]
    ├── [email protected]
    ├── [email protected]
    ├── [email protected]
    ├─┬ [email protected]
    │ └── [email protected]
    └── [email protected]
    ```

    ### 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