Signup/Sign In
LAST UPDATED: SEPTEMBER 27, 2021

Installing Node.js - What are package.json and dependencies in Node.js

    In my last article, I covered the introduction to Node.js and in this article, we will learn about Node.js installation. To develop a Node.js application we require the following SDK setup in our local machine:

    1. Node.js

    2. Node Version Manager (NVM)

    3. Node Package Manager (NPM)

    4. IDE(Integrated Development Environment) or TextEditor.

    The NPM is included in Node.js installation so no need to install it separately.


    Installing Node.js on Windows

    Node.js can be downloaded from its official website Node.js.

    On clicking the above link you will reach this page where it will automatically display the download link as per your Operating System.

    Installing Node.js

    Download Node.js Installer for Windows or if you have any other operating system, download for it.

    After you download the MSI, double-click on it to start the installation as shown below.

    Node.js installation setup

    Click Next to read and accept the License Agreement and then click Install. Click on the Finish button to complete the installation.


    Verifying the installation

    To verify the installation open the command prompt and type the command node -v. If Node.js is correctly installed then it will show the current version of Node.js installed on your computer.

    Verifying Node.js insatllation

    Verifying Node.js version on the terminal on windows.


    Installing/Use different versions of Node.js

    To install a different version of Node.js, we can use the Node Version Manager. For Windows machine, one can download and install the Node Version Manager from this link - Github (Download the nvm-setup.zip file). The Node Version Manager was created with the sole purpose of enabling the developers to install and use any particular version of Node.js whenever they want, without much hassle.

    Once you have successfully installed the Node Version Manager, you can run the following commands to install any version of Node.js you want.

    For example, to install the latest version:

    nvm install latest

    To install some other version, we can specify the version, for example,

    nvm install 5.5.1

    To verify that the installed version is up and running use:

    node --version

    of a shorter version of the above command,

    node -v

    If it works fine, it will return the current version of Node.js

    Using different versions of Node.js

    You can use different versions of Node.js using the command:

    nvm use [verison-number]

    where [version-number] should be the version number that you wish to use.


    What is the Node Package Manager?

    Node Package Manager(NPM) is just like the <script> tag used in an HTML document. The <script> tag allows you to use JavaScript, jQuery or any other JavaScript library in the front end. Similarly, NPM allows you to use the libraries (packages) available in the repository on the official NPM website. All you have to do is install them using the command-line tool.

    NPM provides two main functionalities:

    1. It is an online repository for node.js packages.

    2. It helps in version management and dependency management of the Node.js packages.

    In the current versions, the NPM is included in Node.js installation so you don't need to install it separately.

    NOTE: npm is a sperate project from Node.js which is frequently updated. To update the version of npm, you can use the following command:

    npm install npm@latest -g

    Installing NPM packages

    You can use the command line to install any package locally or globally using the npm command, for example,

    npm install package-name

    For example, to install express package use,

    npm install express
    

    In the snapshot below, you can see the output of the command above,

    Installing Express package using npm

    In local mode, packages are installed for a particular project being built using Node.js, in such case you will find a directory named node_module in you project directory which contains information about all the installed packages for that project.

    Node modules

    For packages installed globally, you can find those packages in {prefix}/lib/node_modules directory, where {prefix} is generally, /usr/ (in linux) or /usr/local/ directory. For Windows OS, you can find the node_modules directory at the location %USERPROFILE%\AppData\Roaming\npm\node_modules.

    To install npm packages globally we should use the --global flag, for example, if we want to install UglifyJS(a tool used for JavaScript minification),

    npm install uglify-js --global
    

    To see what all packages are installed globally, you can use the following command,

    npm list --global

    The package.json File

    All the npm packages contain a file (usually in the project root) called package.json(Javascript Object Notation) in which all the relevant information about the project is stored starting from its name, its author name, its dependencies, version number, description, license, configuration, etc.

    A sample package.json file looks like:

    Package.json


    Naming a Node.js Project

    Some rules for giving the names to your package are:

    1. The name must be less than or equal to 214 characters.

    2. The name can't start with a dot(.) or underscore.

    3. The name can't be in uppercase.

    4. The name should not contain any non-URL(like &, *, etc) characters as it becomes the part of URL.

    To create package.json use the following command,

    npm init

    If for a project, while installing a new package, using --save with the npm install command will automatically take the package name and version and save the dependencies in the project's package.json file.

    Note: While naming your project make sure that the project name is not the same as that of any of the NPM libraries that your project will use as a dependency. If you do, it will receive an error upon attempting to install the dependency. To correct this problem, you will have to access the package.json file and modify the name property.


    Dependencies of a Node.js Project

    Dependencies give the information about the package name and its version numbers which are used in our project. It is an object where key refers to the package name and value refers to the version number of the package. For example,

    Dependencies in node.js project

    Some details about the version ranges:

    • version: Must match version exactly
    • >version: Must be greater than version
    • ~version: Approximately equivalent to version
    • ^version: Compatible with version
    • *: Matches any version

    where the version is a numeric version number or a combination of numbers separated by dots.


    IDE(Integrated Development Environment) or TextEditor

    You can use an IDE or text editor to develop a Node.js application. Node.js application uses Javascript to develop an application. Some of the IDE's that support auto-complete features for Node.js application are Visual Studio, Sublime Text, Eclipse, etc.

    You may also like:

    Working as an Application Developer. I love to learn and discover things JavaScript, HTML, and CSS.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS