Last updated: May 25, 2023
Reading time·4 min
cd
To npm install packages to a specified directory:
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.
mkdir -p ./path/to/folder/node_modules
On Windows, you would use the following command.
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.
node_modules
directory in advance if you use npm
version 6.0 or higher.--prefix
option to npm install
modules in the specified
directory.npm install --prefix ./path/to/folder <YOUR_PACKAGE>
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.
npm install --prefix ./path/to/folder
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.
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.
npm init -y
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.
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.
npm install --prefix ./path/to/folder <YOUR_PACKAGE>
cd
An alternative approach is to use the cd
command to change to the specified
directory and then use npm install
.
cd ./path/to/directory && npm install
Here is an example of using the command on Windows.
And here is an example that specified a relative path that spans multiple directories.
cd ./Public/bobbyhadz && 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.
The examples above install all modules in the specified directory.
If you only need to install a single module, make sure to specify it.
cd ./path/to/directory && npm install <YOUR_MODULE>
And here is an example that does that on Windows.
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.
# macOS and Linux cd ./path/to/directory && npm install <YOUR_MODULE> && cd -
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.
# Windows cd ./path/to/directory && npm install <YOUR_MODULE> && ../../
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".
You can learn more about the related topics by checking out the following tutorials: