Open In App

How to install modules without npm in node.js ?

Last Updated : 05 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

We can install modules required for a particular project in node.js without npm, the recommended node package manager using yarn. Yarn is a wonderful package manager. Like npm, if you have a project folder with package.json containing all the required dependencies mentioned for the project, you can use yarn to install all the dependencies.

 

1. How to install yarn? 
 

To install yarn, visit the official installation page of yarn (https://classic.yarnpkg.com/en/docs/install). The page will automatically detect the operating system you are using. Additional installation instructions are also mentioned in the installation page. Once you have followed the steps on the installation manager and the installation process is complete, type the following command in the ternimal/ command prompt.

 

yarn --version 

 

This should show the particular version you are using in you computer. For example: 1.22.5 . Now that we have installed yarn, lets see how we use yarn in our projects.

2. How to use yarn to install projects? 
 

To use yarn, go to the folder where the modules are needed to be installed. If it is not initialized with yarn, run the yarn init command. It will ask some questions regarding the project to create the package.json file. The package.json file is the most important file as it contains the necessary modules that are required by your project. Anyone with the package.json file, can run some commands( we will discuss this later) to install all the dependencies required by your project. You will get a similar question when you run yarn init command:

 

question name (testdir): my-awesome-package
question version (1.0.0):
question description: 
    The best package you will ever find.
question entry point (index.js):
question git repository: 
    https://github.com/yarnpkg/example-yarn-package
question author: Yarn Contributor
question license (MIT):
question private:
success Saved package.json
Done in 87.70s.

 

After this is done, a package, json file is created. If you open the package.json file, it should look something like this:

 

{
     "name": "my-awesome-package",
     "version": "1.0.0",
     "description": "The best package you will ever find.",
     "main": "index.js",
     "repository": {
           "url": "https://github.com/yarnpkg/example-yarn-package",
           "type": "git"
     },
     "author": "Yarn Contributor",
     "license": "MIT"
}

 

Alternatively, if you have a project that contains a  package.json file from the beginning, you can use yarn or yarn install command to install all the mentioned dependencies from the package.json file.

 

Note: If you do not want to answer all the questions when performing yarn init command (although not recommended), you can use yarn init -y command to initialise it with the default values. You can change the details by editing the package.json file with suitable text editor. 

3. Installing packages in the project folder 
 

Now we shall see how we can install packages using yarn. Suppose we want to install the package named express. We would enter the following command to install express: 

 

// Command to install express to the current project folder
yarn add express  

// Command to install express globally in your machine
yarn global add expres 

// This is the most generalized way, Just replace  
// the <package-name> with the name of the package
yarn add <package-name> 

 

Note: The global keyword is used to inform yarn that we want to install express globally. 

Reference: There are a lot more commands that you can execute with yarn. A list of commands is mentioned in the following link: https://classic.yarnpkg.com/en/docs/cli 

If you are migrating from npm to yarn, you can use this cheatsheet to know the similar commands for yarn. 

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads