Personal note about NPM learning journey
NPM is a registry, command line interface, and a website. The registry is where ‘packages’ is hosted so that people can download it. The command line interface (CLI) is the program we use to interact with the registry. The website is where we search for a package and where we can do administration task regarding the package we hosted on registry.
Package
A package is a folder with package.json
file inside it. This file will contains the information of the package such us the name, version, and the main file. Main file is the file used when someone is importing your code using require
keyword in javascript.
Publishing a package
To publish a package, you must ensure that you are logged in using CLI:
npm login
Then, once the package is complete. You can publish using:
npm publish
Installing a package
You can install a package to your project in two ways.
Installing a package using CLI
When you build the software, most likely you will need someone else’s code (package) in registry. To download that package, use:
npm install module-name@version
Installing a package by adding package dependency to package.json
Add a field called ‘dependencies’
{
"name": "my_package",
"version": "1.0.0",
"dependencies": {
"my_dep": "^1.0.0",
"another_dep": "~2.2.0"
}
}
Module
Module could be a:
- Folder with package.json
- A javascript file
When you use a module in your code, you will call require('module-name')
in the script.