How to Save a Dependency Module or Package in package.json file

Posted on

This is an article to explain the command for installing dependency packages or modules exist in the definition of the file with the name of ‘package.json’.  There is an additional explanation which is actually exist in this link. But before going on to the explanation, below is the actual case of the command usage :

  1. There is an application where the application itself is a Node.js application. Furthermore, after creating the applicationi using ‘npm init -y’, there will be a file with the name of ‘package.json’. In that file, there is a specific definition for dependencies. The folowing is the content of the folder using Node.js template application :

C:\node\app>dir
 Volume in drive C is Windows
 Volume Serial Number is E003-3593

 Directory of C:\node\server

04/16/2020  08:48 AM

. 04/16/2020 08:48 AM.. 04/16/2020 08:48 AM 220 package.json 1 File(s) 220 bytes 2 Dir(s) 195,068,497,920 bytes free C:\node\chat-app\server>

Actually, there is only one file in the directory after running ‘npm init -y’ to initialize and create ‘package.json’ file. Soon after that, there can be an additional package or module for the Node.js application. The requirement for the additional package or module have to exist in the ‘dependencies’ section. After adding and defining the package or module in the dependencies section, before Node.js version 5, there is an additional command for installing and adding the package or module dependencies. It is by executing the following command :

npm install --save

The execution sample exist in the following command execution :

C:\node\server>npm install --save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN server@1.0.0 No description
npm WARN server@1.0.0 No repository field.

up to date in 1.093s
found 0 vulnerabilities

C:\node\server>

After executing the above command, there will exist another file with the name of ‘package-lock.json’. But as in the previous information, there is already a link specifying the explanation of the command ‘npm install –save’. The following is the complete explanation on that link :

As of npm 5.0.0, installed modules are added as a dependency by default, so the –save option is no longer needed. The other save options still exist and are listed in the documentation for npm install.

Before version 5, NPM simply installed a package under node_modules by default. When you were trying to install dependencies for your app/module, you would need to first install them, and then add them (along with the appropriate version number) to the dependencies section of your package.json.

The –save option instructed NPM to include the package inside of the dependencies section of your package.json automatically, thus saving you an additional step.

In addition, there are the complementary options –save-dev and –save-optional which save the package under devDependencies and optionalDependencies, respectively. This is useful when installing development-only packages, like grunt or your testing library.

Leave a Reply