Open In App

How to install specified directory using npm ?

Improve
Improve
Like Article
Like
Save
Share
Report

Node JS is a platform built on Chrome’s JavaScript v8 engine, which is used for easily building fast and scalable network applications. Javascript uses an event-driven, non-blocking I/O model that makes it lightweight and efficient which is perfect for data-intensive real-time applications. It runs across distributed devices and makes use of the tools (or packages) in Node. js.

We need to install it on our machine and manage it in a useful way. This is where npm i.e. Node package Manager, comes into play. It contains default packages and also allows us to install external modules and packages in our project. We want to use and provide a user interface to work with them.

Let’s look at the step-by-step process to install specified directory using npm.

Steps to Install npm to a Specific Directory

Step 1: First, check if Node is installed on your PC or laptop. To check if Node is pre-installed or not open up a terminal on Mac or a command prompt in Windows and type the following command :

node -v

Now, if the Node version is displayed something like “

v12.18.3″

then you may conclude that Node is pre-installed on the PC or laptop, if not installation of Node.js on Windows according to your PC requirement and version and you can also install the previous version of node.js.

Step 2: Open up a javascript project in an editor and decide on which directory you want to install npm package.

To create sub-directory:

we can also create a sub-directory and install npm packages, to create a sub-directory “cd”(change directory) into the project directory and make use of mkdir command,

To create new folder:

To create a new Folder/directory we can use mkdir <folder name > and to make a sub-directory or sub-folder inside a directory/folder use command mkdir -p <folder name>.

Here, I will be creating a directory named “new” under my “TEST” project folder in which I will install my npm package.So, I will be using the command in terminal  “mkdir -p new” once our directory in the terminal is set to our Project directory i.e “TEST” and we can see our project files are structured somewhat in this manner under the “TEST” main directory.

Install-npm-to-a-Specific-Directory_1

Step 3: Now, our final step to install npm to a specific directory will be to make use of the –prefix option, Here we will be making use of the following command to install npm to a specific directory.

npm install --prefix ./(folder/sub_folder_name) <package name>

Installing npm package “animation” in the current directory and using below command:

npm install --prefix ./new animation

Console Output:

Install-npm-to-a-Specific-Directory_2

Now, as we have installed our required npm package into our desired sub-directory i.e “new”, we can check our “new” directory by opening it and we can see that the following package is successfully installed into the directory.

Updated Project structure: The following screenshots shows that package is successfully installed into the directory.

Install-npm-to-a-Specific-Directory_3

Also Read


Last Updated : 25 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads