To initializing a project with the node it is used npm init, a number of instructions will be shown where you will find: description, entry point (the main file where the code will come from), test command, in the package Jason will be settled; keywords; and author they must be indicated.
The shortcut npm init -y that will create a common Jason that can be modified when it is wished.
To install a package from npm the command used is npm i <package-name> -D || -g, the “-D” will download, saved as developing a dependency, and “-g” will do it globally available for all repositories i.e. nodemon. In this case, it will need the sudo command due to its global scope.
A command to see the package you have installed globally is npm list -g –depth 0. It is important to ignore the modules when they are created with gitignore .
DEPENDENCIES WITH FORCE
The command –dry-run it simulates the output of the installation. The command -force/f this will force the installation from the last version npm server contains.
The command npm install will install all the packages inside the package Jason no matter the flags put before. In the case it is know the version of the package that is whished to install the command “[email protected]<version>”.
UPDATE AND DELETE DEPENDENCIES
• npm list: it simulate a hierarchy of the packages downloaded
• npm outdate which packages can be update.
• Npm outdate –-dd: if it is whished to see the information of an output more detailed.
• Npm update: it will update those packages out of date.
• Npm install <package-name>@latest: it will download the latest version of a specific package.
• npm uninstall <package-name>: it will uninstall the package selected
• npm uninstall –no-save: it will not deleted from the package Jason.
In visual studio code is recommended the download of the npm extension, this will notify if the installed package is in both package.json file and nodules folder.
This package power the information of the package Jason, this last one indicates us the version updated it is being used with “^” and “~”, catch and patch respectively.
In the package Lock the code can see the version used, a description about that version and it respectively links to that package.
Scripts saved commands under properties names we settle to execute tasks we want to. It would find it similarities with the alias creation in git.
To habilitate new scripts is needed to be register and created in the package Jason of the repository.
It is important to have in mind that there can be errors due to typos, initializing process bad done, etc.
Here to detect problems is important to use the extension of commands - - dd so we will be able to see the details done in the process.
Also, the same terminal can send you a file with a lot information about the error. It can be taking the URL and then open with a Code Editor to see all the information.
npm cache clean –force, and npm cache verify, as it said it will clear the cache existed. rm -rf <package-folder> this command will delete the specific folder, it is a dangerous command.
We must ensure that malicious software’s are not installed. Npm have a tool that allows us to detect those vulnerabilities, with npm audits will show us a list and recommendations.
Normally these vulnerabilities exist because updates or malicious software. It can be updated one by one or npm audit fit to update all the packages automatically or snyk to stay update about the dependencies used.
Once in the whished route we want to import, it is really important to initialize with git init and after with npm init.
In the package the code normally resides inside a folder “src” where it is the file “index.js” that will contain the main code of the package.
In the root there must be created a folder that will be called to initialize the index.js . This last one must be export to this “bin” folder. The bin folder contains the “global.js” will had in titile: #! /usr/bin/env node.
Finally, in the package Jason it have to be added after the license, “bin” property as value the position of the global.js and lately “preferGlobal” property as true.
In the folder that we want to publish we write the command npm link to install it globally in the system of our computer.
Another option is with npm install -g <route-directory> and also will be update if a changed had been made.
Once ready it is time to publish it in npm platform, in the terminal npm adduser and npm publish to finally uploading it.
Once published the package is important to apport a good README.md and a connection to a repository so it is available to make pull-request. A standard structure description would be:
If it is whished to update the code of the package, the repository must be saved and npm init to finally add npm version <minor|major|patch> and it will update automatically.
Npm publish to update the whole package in the cloud of the npm platform. It is really important to update the packages made quite frequently.