How to automatically build the package.json file for NodeJS Projects

Every Node.JS Project requires package.json file as it contains everything related to project to run, deploy and manage the NodeJS application on cloud or server as it contains Node Module dependencies, node engine required and other important information related to Specific Project. So in This NodeJS How-To Guide, we will discuss How to automatically build the package.json file for NodeJS projects, by using this simple hack you can create your nodejs project’s package.json file easily.

Article Contents

Why you need to build the package.json file automatically for NodeJS projects?

Consider You’re working with Multiple Node.js projects and you need to initiate multiple projects a day and you need to update your project’s package.json file every time you add or remove a module from npm directory and Now imagine this with multiple projects, this process get’s really complex, for that purpose you need to follow this quick hack to automatically build nodejs package.json file for your projects.

**For this you need NPM installed on your computer or workstation. If you have not yet installed NPM, then you can easily install it, by doing a quick google search, and nowadays Nodejs comes combined with Nodejs Installation Package, so you don’t need to worry about installing NPM.

Procedure to Automatically build package.json file in Nodejs

  • Package.json File mainly used by NPM to know all the information about your Node.JS Project
  • You can initialize Package.json file by command npm init
  • This command will guide you through all the common items about your projects like name, version, dependencies etc and Wherever possible suggests default values as you can see in the below GIF.
  • It will ask you about Project name, version, entry point, description, test command, author, license etc
  • after that it will show you your package.json file with all the filled details and ask is it correct and you’re done
  • If you need to add npm module dependencies in package.json file, then you can use this commandnpm install express --save
  • The save flag will add npm module in your package.json file
    vmreddy000@BlueMachine MINGW64 /d/Mean Stack Dev/myProject
    $ 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> --save` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    name: (myProject) nodejs-project
    version: (1.0.0)
    description: something related to your project
    entry point: (index.js) app.js
    test command:
    git repository:
    keywords:
    author: stackFAME
    license: (ISC)
    About to write to D:\Mean Stack Dev\myProject\package.json:
    
    {
      "name": "nodejs-project",
      "version": "1.0.0",
      "description": "something related to your project",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "stackFAME",
      "license": "ISC"
    }
    
    
    

     

  • To add already installed modules to package.json file you can use https://npmjs.com/package/npm-collect package.
  • You can use npm uninstall <pkg> --save   command to remove pkg from package.json file.

NODEJS NPM Console

 

Please Share, comment and Subscribe to our  Newsletter for awesome articles related to Web development and Nodejs Development Tutorials, also check out:

See also  Get List of all files in a directory in Node.js

Leave a Comment