Open In App
Related Articles

How to install specified directory using npm ?

Improve Article
Save Article
Like Article

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 that run across distributed devices and to make use of the tools (or packages) in Node. js, we need to be able to install in our machine and manage them in a useful way. This is where npm, the Node package manager, comes in to play and it contains default packages and also allows us to install external packages in our project. We want to use and provides a user interface to work with them.

Installing npm to a specific directory using the following simple steps :

  1. First, check if Node is installed on your pc or laptop. In order to check if Node is pre-installed or not open up a terminal in mac or 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 then please refer this article and install node according to your PC requirement and version.

  2. Open up a javascript project in an editor and decide on which directory you want to install the npm package, we can also create a sub-directory and install our npm packages, to create a sub-directory “cd”(change directory) into the project directory and make use of mkdir command, 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.

  3. Now, our final step to install 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 our npm package into 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
  4. Console Output:

    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:

Last Updated : 10 Feb, 2021
Like Article
Save Article
Similar Reads
Related Tutorials