javascript – Advantages of organizing dependencies using Node.js / npm

Question:

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

  1. How to download?
  2. What are the different ways to download, either: npm install , npm install -g , npm install –save
  3. Is the package.json file created automatically or does it have to be done manually in each project?
  4. It is necessary to have require.js between libraries to use the require keyword

Answer:

require and module.exports

For starters, the requires.js library has nothing to do with the require() method of node.js. This method is a part of node itself, and it is the backbone of how 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". So, 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
4

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

require() also used to import external packages (or modules), another question from your question.


npm and package.json

npm is node's own tool to assist you during application development. This is why you do not need any IDE to work with node, with a text editor it is enough thanks to the npm command.

package.json is a file in json format (very obvious right?) that contains the package information, including 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 (try 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 type server.js when prompted for the entry point .

dependencies

The command npm install <paquete> searches the node registry for the package (the registry is the official module database), if it exists, it downloads it along with all its direct and indirect dependencies and installs them in a folder in the directory local 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 not have no effect. Another way to do the same would be to edit package.json , add the dependency and then call npm install .

The -g option indicates that the package should be installed globally rather than within node_modules . This option is used to install development tools, such as bower, gulp, etc.

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

Finally, external dependencies (those that are installed inside the node_modules folder) are imported using require('nombre_modulo') , that is, you should NOT include the ./ .

execution and testing

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

Unit test setup example:

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

conclusions

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, it is no longer 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! via npm install <nombre_modulo> .

Scroll to Top