Advantages of organizing dependencies using Node.js / npm


I would like to know what the advantages and disadvantages are (if they are clear), to use Node.js to download the modules, dependencies or packages , from the libraries and frameworks what we will use to develop. To solve my doubts I have a couple of questions.

  • How to download?
  • What are the different ways to download, either: npm install , npm install -g , npm install --save
  • Is the package.json file created automatically or must it be done manually in each project?
  • It is necessary to have require.js between the libraries to use the reserved word require
  • asked by Pedro Miguel Pimienta Morales 14.02.2016 в 00:37

    2 answers


    require and module.exports

    To begin with, the requires.js library has nothing to do with the method require() of node.js . This method is a proper part of node, and is the backbone in the way in which node resolves dependencies.

    Suppose we make an application with two files: server.js and herramientas.js located in the same folder. Where tools.js has functions that you want to import into server.js, the "main program". Then we have:

    // herramientas.js
    var exports = {};
    module.exports = exports;
    exports.sumar = function (a, b) { return a + b };
    // server.js
    var tools = require('./herramientas');
    console.log(tools.sumar(2, 2));
    // Resultado al ejecutar

    This example serves to demonstrate that require('./nombre_modulo') returns the object that was set in the variable module.exports of the file named nombre_modulo.js .

    require() is also used to import external packages (or modules), another one of the questions of your question.

    npm and package.json

    npm is the own tool of node to assist you during the development of the applications. This is why you do not need any IDE to work with node, with a text editor reaches thanks to the command npm .

    package.json is a file in json format (very obvious no?) that contains the package information, including the dependencies, but also information about the version, the author and other herbs.

    You can create it manually with your text editor and with the help of the documentation (test with npm help json ) or you can use the npm init command, which will interactively ask you for the basic data of your package. If you use this option, be sure to write server.js when asking for entry point .


    The npm install <paquete> command searches the registry node for the package (the registry is the basis of official module data), if it exists, download it together with all its direct and indirect dependencies and install them inside a folder in the local directory called node_modules .

    The --save option makes that after downloading the package, update the list of dependencies in package.json , which is very useful because it saves a step, but package.json must already exist in this step and if it does not exist, the option will have no effect. Another way to do the same would be to edit package.json , adding the dependency and then call npm install .

    The -g option indicates that the package should be installed in global instead of node_modules . This option is used to install development tools, such as bower, gulp, etc.

    When installed globally, allow commands to be added to your system console (eg npm install bower -g install a new system command called bower )

    Finally, external dependencies (those that are installed inside the node_modules folder) are imported using require('nombre_modulo') , meaning that NO you must include the ./ .

    execution and tests

    After building the application and declaring the dependencies with npm start you execute your application. You can also declare the unit tests of your module and launch them with npm test .

    Example of unit test configuration:

    "scripts": {
        "test": "<comando que hace la prueba>"


    As you can see, npm has many advantages. For example, it avoids having to manually download all the dependencies of a package, which, as a project grows, ceases to be practical / scalable. Also, if your package is a library that you are going to publish in the registry, you have the command npm publish <nombre_modulo> that publishes (or updates) your package on the internet so that anyone can use it ... of course! by npm install <nombre_modulo> .

    answered by 14.02.2016 / 08:24

    To extend the great response of @rnd, if you would like to use the NodeJs modules to be used from the client side, you can use browserify , in that way, you have the advantages of handling npm dependencies on the browser side.

    answered by 24.04.2016 в 01:48