A package manager is a system that will manage your project dependencies.
- npm is the world's largest Software Library (more than 11 million developers worldwide and more than one million packages). The free npm Registry has become the center of JavaScript code sharing.
- npm is also a software Package Manager and Installer
- npm Inc is the company behind the npm Registry and npm CLI.
- npm is free to use. https://www.npmjs.com/products
- You can download all npm public software packages without any registration or logon.
- Yarn is an alternative package manager created by Facebook.
- The main difference between NPM and Yarn is the package installation process. Yarn installs packages in parallel. Yarn is optimized to fetch and install multiple packages at once. NPM will perform a serial installation process.
- npm is installed with Node.js. This means that you have to install Node.js to get npm installed on your computer.
- Download Node.js from the official Node.js web site: https://nodejs.org.
npm install <package>- All npm packages are defined in files called package.json.
- Please check the semver docs
- The content of package.json must be written in JSON.
- At least two fields must be present in the definition file: name and version.
{
"name" : "foo",
"version" : "1.1.1",
"description" : "A package for fooing things",
"main" : "foo.js",
"keywords" : ["foo", "fool", "foolish"],
"author" : "John Doe",
"licence" : "ISC"
}- npm foster semantic versioning specs. It is a good practice for healthy, reliable, and secure packages.
- Let's check the npm terminology
{
"scripts": {
"start": "node app.js",
"test": "jest ./test",
"hello-world": "echo \"Hello World\""
}
}- To run multiple scripts sequentially, we use
&&. For example:npm run lint && npm test - To run multiple scripts in parallel, we use
&. For example:npm run lint & npm test. (for UNIX environments)
We can create "pre" and "post" scripts for any of our scripts, and NPM will automatically run them in order
{
"scripts": {
"prehello": "echo \"--Preparing greeting\"",
"hello": "echo \"Hello World\"",
"posthello": "echo \"--Greeting delivered\""
}
}"scripts": {
"start": "NODE_ENV=test node main.js"
},Then use process.env.NODE_ENV in your app.
npm run <command> [-- <args>]npm run start -- --foo=3console.log('process.argv', process.argv);