How to npm install packages to a specified directory

avatar
Borislav Hadzhiev

Last updated: May 25, 2023
4 min

banner

# Table of Contents

  1. How to npm install packages to a specified directory
  2. Install NPM packages to a specific directory by using cd

# How to npm install packages to a specified directory

To npm install packages to a specified directory:

  1. Create the node_modules directory as a subdirectory to the path.

For example, if you want to npm install packages in a ./path/to/folder directory, you have to create ./path/to/folder/node_modules.

You can use your terminal to create the directory.

On macOS and Linux, use the following command.

shell
mkdir -p ./path/to/folder/node_modules

On Windows, you would use the following command.

shell
mkdir "./path/to/folder/node_modules"

The node_modules folder in the directory in which you want to npm install modules has to exist to be sure that npm won't look for a node_modules directory higher up the tree.

Make sure to replace ./path/to/folder with the path to the folder in which you want to npm install modules.

Note that you don't have to create the node_modules directory in advance if you use npm version 6.0 or higher.
  1. Use the --prefix option to npm install modules in the specified directory.
shell
npm install --prefix ./path/to/folder <YOUR_PACKAGE>

npm install package to specific directory

Make sure to replace the path with your preferred path and the <YOUR_PACKAGE> placeholder with the name of the package you want to npm install.

If you want to npm install all packages from the package.json file in the specified directory, use the following command instead.

shell
npm install --prefix ./path/to/folder

npm install all packages to specific directory

Notice that we aren't specifying a package to install after the path.

This installs all packages that are specified in the dependencies and devDependencies object of the package.json file in the specified directory.

If you encounter issues when using this approach, try to create the package.json file in the specified directory in advance.

You can cd into the directory in which you want to npm install modules and run the following command to initialize a package.json file.

shell
npm init -y

initialize package json before installing packages

Make sure you have npm version 3.8.6 or higher to be able to use the --prefix option.

You can use the npm --version command to check your npm version.

shell
npm --version

check your npm version

If you use an npm version 6.0.0 or higher, you don't have to create the node_modules directory or the package.json file in advance.

Running the following command is sufficient.

shell
npm install --prefix ./path/to/folder <YOUR_PACKAGE>

npm install package to specific directory

# Install NPM packages to a specific directory by using cd

An alternative approach is to use the cd command to change to the specified directory and then use npm install.

shell
cd ./path/to/directory && npm install

Here is an example of using the command on Windows.

change to specified directory and npm install

And here is an example that specified a relative path that spans multiple directories.

shell
cd ./Public/bobbyhadz && npm install

specify relative path and npm install

Here is an example of using the cd command to change to a specific directory and installing your npm modules on macOS and Linux.

cd into specific directory and install npm modules on macos and linux

The examples above install all modules in the specified directory.

If you only need to install a single module, make sure to specify it.

shell
cd ./path/to/directory && npm install <YOUR_MODULE>

change to directory and npm install specific module

And here is an example that does that on Windows.

windows cd into specific directory and npm install specific module

If you are on macOS or Linux, you can use the cd - command to change to the previous directory after installing your modules in the given directory.

shell
# macOS and Linux cd ./path/to/directory && npm install <YOUR_MODULE> && cd -

change back to previous directory after installing modules in given directory

The cd - switches your terminal to the old PWD (present working directory).

Notice that the terminal's present working directory is Desktop at the start and end of running the command.

If you are on Windows, you can use the following command instead.

shell
# Windows cd ./path/to/directory && npm install <YOUR_MODULE> && ../../

windows cd into directory npm install-and-go-back

The ../ prefix means "go one directory up", make sure to use the prefix as many times as necessary.

For example, ../../ means "go 2 directories up".

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.