Friday 16 January 2015

How to create a package.json file using npm

node js, nodejs

The package.json file identifies a project, who authored it and which file is the entry point. This file will also keep track of the module this project depends on.







So first open terminal / command line and create a directory (i.e. subhro) where you want to create the package.json file.

Next change to the created directory.

To generate a package.json file type "npm init" on command line and press enter

npm init


after you press enter it will prompt you to enter a name. If you do not provide any name it will take the default name which is the name of the directory you are working on.

name: (subhro)

Next it will ask for the version and you can keep it empty to take the default.

version: (0.0.0)

Next it will ask for a description. Type a description of your own.

description: Keeps track an train's time

Next one is the entry point. Every project that you create should have one entry point. You can also accept the default which is index.js.

entry point: (index.js)

Then it will ask for test command, later on you can automatically set this up for unit test. For now on you can keep it blank.

test command:

After test it allows you to set path to a git repository, you can keep it blank if you are using it for yourself unless you plan to distribute your project to someone else.

git repository:

 Next one is keywords, this is also for distributing the project, so you can keep it blank for yourself.

keywords:

Next it will prompt for author, enter your name and email address (in angle brackets>, as I am entering mine and email.

author : "Subhroneel Ganguly <subhro1976@gmail.com>"

Then it will prompt for license, you can enter any license you like, or you can also use default as I have done here.

license: "BSD-2-Clause"
 

After you press enter the following preview of package.json file and then asking for confirmation. You can press enter if you want to add it or type no and press enter if you don't want to.

 {
  "name": "subhro",
  "version": "0.0.0",
  "description": "Keeps track an train's time",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Subhroneel Ganguly <subhro1976@gmail.com>",
  "license": "BSD-2-Clause",
  "dependencies": {
    "express": "~3.4.8"
  }
}


Is this ok? (yes)

After this it will return back to command prompt. Now check for whether package.json file is been created and can also look for the content of the file.

No comments: