5 Things You Need To Know About Node Package Manager
What is npm?
Also known as npm (a bacronymic or retroachronym abbreviation for "npm is not an acronym") is one the most popular tools for web development using Node.js. Because It help us search, organize and install quickly modules that we are going to use in our project, and this we can do it with only typing a line of code in the terminal.
For the ones who don't know a module or package in a development context, it's basically a group of functions and processes that helps you do more in less time. One example it's nodemon, this module restarts the local server every time you save a change in a .js file, so you don't need to do it manually.
Therefore, one of the main problems that solves npm is the waste of time, specially, searching that package that is going to make work your project in a certain environment or help you create a more efficient solution. Also lets you upload packages to its repository for your use and everyone else.
How do you install it?
For Windows:
- Go to https://nodejs.org/en/download
- Select the installer of your choice and download it
- Execute and follow the steps in the installer an make sure your that the npm is going to be install
- When is done, open your terminal
- Type: npm --version
- It will appear a text that shows the npm version
Commands's Shortcuts
With lastest versions npm, its efficiency has improve a lot by allow it us to make the common actions with less writing. Here I show you ones of the most popular actions with their complete and short structure:
Version Control
When you are working on a project and you are constantly making changes and deploying, sometimes you are going to get an error without an aparent cause, It's very uncommon but it can happen because of the versions of you dependencies.
You see, when you install a npm package with the classic command (npm install <package's name> ) it has this structure in the package.json file:
- "<package's name>" : "^<mayor version>.<minor version>.<fix and correction versions>"
- For example in the case of nodemon we get something like this: "nodemon": ^"2.0.6"
Everything is normal until you see this simbol "^" know as caret or cincunflex accent, in development works in the following manner. It tells to npm that everytime you install the dependencies because you are going to deploy your project on a server or use it in another device. It's going to download the dependencies in the package.json file but only maintaining the mayor version.
In other words, it means that if you start to install your dependencies in other device and nodemon in that moment launchs a minor or fixed version, your npm is going to take it an the result is going to be "nodemon": ^"2.1.0" in your package.json.
That is why you are going to need to take into account these symbols, that's right, these, because there is another one "~" the tilde or in spanish virgulilla, that is going to handle only the fix versions.
To solve this problem you can use these commands:
Specific version
Exact actual version (install without ~ or ^)
Latest version
Most useful commands
Here I share with you a list of the commands that are going to be useful for your projects:
npm is a great tool that every JavaScript developer needs to handle, it had helped me a lot to completed projects faster and deploy them without much worry about dependencies. Once you learn to handle carefully the version control, your going to use it everytime you work with JavaScript.
References
- npm official documentation: https://docs.npmjs.com/
- Node.js: https://nodejs.org/
- Nodemon: https://nodemon.io/
- W3Schools npm section: https://www.w3schools.com/nodejs/default.asp
- npm scripts list: https://jaketrent.com/post/list-npm-scripts/