1. Node Package manager is a package manager which ships along node
  2. It is a Javascript package manager which helps to manage modules like HTTP, Event etc..
  3. Once the npm init is run package.json would be created in the project folder.It is similar to pom.xml for Maven project which saves all the settings and dependencies
  4. To check the npm version
    >>npm -v 
    >>npm --version
    
  5. To create a new project we use the below command – take default setting by telling -yes
    >>npm init -yes
    
  6. To Install a new package is as below
    >>npm install NAME_OF_PACKAGE
    
  7. To Set particular config name
    >>npm set CONFIG_KEY CONFIG_VALUE
    >>npm set init-licencse "MIT"
    
  8. To Get particular config name
    >>npm get CONFIG_KEY 
    >>npm get init-licencse
    
  9. To Install a new package
    >>npm install packagename
    >>npm install loadash
    

    Running the above command is going to create a node_modules folder in project with the dependencies for the package(loadash) installed.

  10. To Install only dependencies that are needed to run app other than utility dependencies like gulp which provides minified version of JS use below command
    >>npm install --production
    
  11. To remove dependencies
    >>npm remove gulp
    
  12. To install specific version
    >>npm install gulp@4.7.3
    
  13. To update latest version
    >>npm update gulp
    
  14. Semantic versioning formart.PatchVersion includes bug fixes and minor tweaks
    MajorVersion.MinorVersion.PatchVersion
    4.7.8
    
  15. How to find the location of node modules
    >>npm root -g
    

    This will take you to location C:\Users\Mugil\AppData\Roaming\npm\node_modules

  16. How to install server globally – live-server will reload the JS files without restart of server
    >>npm install -g live-server
    

    This will take you to location C:\Users\Mugil\AppData\Roaming\npm\node_modules

  17. When you move the project to new environment no need to move the node_modules folder similar to avoiding JAR while shifting java application.We just need to move the JS file and package.json which contains the details of dependencies. Running npm install will read the package.json file and load the dependencies in new environment

package.json

   "name": "my_package",
    "description": "",
    "version": "1.0.0",
    "main": "index.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1"
    },
    "dependencies": {"loadash" : 14.7.8},
    "keywords": [],
    "author": "",
    "license": "ISC",
    "bugs": {
      "url": "https://github.com/ashleygwilliams/my_package/issues"
    },
    "homepage": "https://github.com/ashleygwilliams/my_package"
  }

How to run some js file in the beginning of the server
I have JS server which needs to start at the beginning of application. In such case I would be defining the JS file in scripts.
You need to call npm start after chaning package.json file
package.json

.
.
"scripts": {
      "start": "node server.js"
    }
.
.

Let’s create a simple application which greets with Welcome message in console on start
package.json

{
  "name": "school",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "Mugil",
  "license": "ISC"
}

index.js

console.log('Welcome to School .....');

output

Welcome to School .....

package.json
live-server allows you to run code without restarting server after changes.

.
.
"scripts": {
    "start": "node index.js",
    "server": "live-server"
  }
.
.
>>npm run server

Comments are closed.