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
4
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
.
dependencies
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>"
}
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, 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>
.