Monday, 1 October 2018

npm: Adding dependencies to node.js application

All node.js applications require a package.json file, you can specify metadata and all the dependencies required by the application in this file.

How to create package.json file?
Go to the application root directory and execute ‘npm init’ command.

C:\Users\Public\webCalculator>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (webcalculator)
version: (1.0.0)
description: Application to perform arithmetic calculations
entry point: (index.js) app.js
test command:
git repository:
keywords: calculator, arithmetic operations
author: Krishna
license: (ISC) MIT
About to write to C:\Users\Public\webCalculator\package.json:

{
  "name": "webcalculator",
  "version": "1.0.0",
  "description": "Application to perform arithmetic calculations",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "calculator",
    "arithmetic",
    "operations"
  ],
  "author": "Krishna",
  "license": "MIT"
}


Is this OK? (yes)

‘package.json’ looks like below.

package.json

{
  "name": "webcalculator",
  "version": "1.0.0",
  "description": "Application to perform arithmetic calculations",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "calculator",
    "arithmetic",
    "operations"
  ],
  "author": "Krishna",
  "license": "MIT"
}

How to add a package to package.json file?
Execute below command.
npm install {PACKAGE_NAME} –save

Example
npm install express --save

Above statement installs express module and add the dependency in package.json file.

C:\Users\Public\webCalculator>npm install express --save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN webcalculator@1.0.0 No repository field.

+ express@4.16.3
added 50 packages from 47 contributors and audited 119 packages in 11.727s
found 0 vulnerabilities

package.json file is updated like below.


package.json

{
  "name": "webcalculator",
  "version": "1.0.0",
  "description": "Application to perform arithmetic calculations",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "calculator",
    "arithmetic",
    "operations"
  ],
  "author": "Krishna",
  "license": "MIT",
  "dependencies": {
    "express": "^4.16.3"
  }
}


As you see ‘package.json’ file, express dependency is added to the file and you can observe ‘node_modules’ folder is created in application root directory and all the dependencies are copied there.

How to remove the dependency?
Use below command to remove a dependency.
npm remove {PACKAGE_NAME} --save

For example, below command remove the express module.

‘npm remove express --save'

C:\Users\Public\webCalculator>npm remove express --save
npm WARN webcalculator@1.0.0 No repository field.

removed 50 packages in 3.503s
found 0 vulnerabilities

package.json is updated like below.


package.json

{
  "name": "webcalculator",
  "version": "1.0.0",
  "description": "Application to perform arithmetic calculations",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "calculator",
    "arithmetic",
    "operations"
  ],
  "author": "Krishna",
  "license": "MIT",
  "dependencies": {}
}

How to ship my node.js application?
While shipping node.js application, you no need to ship all the dependent libraries. You can ship application source code along with package.json. User can execute ‘npm install’  command in application root directory to download the dependencies.

Previous                                                 Next                                                 Home

No comments:

Post a Comment