I would like to know what the advantages and disadvantages are (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 resolve 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 does it have to be done manually in each project?
- It is necessary to have require.js among the libraries to use the require keyword
require and module.exports
For starters, the requires.js library has nothing to do with the
require()
node.js method . This method is a part of node itself, and is the backbone of how node resolves dependencies.Suppose we make an application with two files:
server.js
andherramientas.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:This example serves to demonstrate that it
require('./nombre_modulo')
returns the object that was set in the variablemodule.exports
of the named filenombre_modulo.js
.require()
it is also used to import external packages (or modules), another issue in your question.npm and package.json
npm
is node's own tool to assist you during application development. This is why no IDE is needed to work with node, with a text editor it is enough thanks to thenpm
.package.json
is a file in json format (very obvious isn't it?) that contains the information of the package, including the dependencies, but also information about the version, the author and other things.You can create it manually with your text editor and with the help of the documentation (try with
npm help json
) or you can use the commandnpm init
, which will interactively request the basic data of your package. If you use this option, be sure to typeserver.js
when prompted for theentry 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 into a folder in the local directory callednode_modules
.The option
--save
makes that after downloading the package, update the list of dependencies inpackage.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 thing would be to editpackage.json
, add the dependency and then callnpm install
.The option
-g
indicates that the package should be installed globally instead of insidenode_modules
. This option is used to install development tools, such as bower, gulp, etc.By installing globally, it allows commands to be added to your system console (eg
npm install bower -g
install a new system command calledbower
)Lastly, external dependencies (those that are installed inside the folder
node_modules
) are imported usingrequire('nombre_modulo')
, meaning you should NOT include the./
.execution and testing
After the application is built and the dependencies declared,
npm start
you run your application. You can also declare unit tests for your module and launch them withnpm test
.Unit test setup example:
conclusions
As you can see, it
npm
has many advantages. For example, it prevents you from having to manually download all the dependencies of a package, which as a project grows, becomes impractical/scalable. Also, if your package is a library that you are going to publish in the registry, you have the commandnpm publish <nombre_modulo>
that publishes (or updates) your package on the internet so that anyone can use it... of course! throughnpm install <nombre_modulo>
.To extend @rnd's great answer, if you wanted to use NodeJs modules to be used from the client side, you can use browserify , that way you have the advantages of npm's dependency management on the browser side.