To set a custom download path for npm packages, you can adjust your npm configuration or modify environment variables. Here’s how to do it:
Using npm config command
- Set Custom Directory Globally
- Run the following command in your terminal to set a global custom directory:
npm config set prefix ‘/path/to/your/custom/directory’ —global -
Replace /path/to/your/custom/directory with your desired path.
-
Set Custom Directory for User Scope
-
If you want to set the custom directory for your user account only, omit the —global flag:
npm config set prefix ‘/path/to/your/custom/directory’ -
Verify Configuration
- Check if the configuration was applied correctly by running:
npm config get prefix - This should output your custom directory path.
Using NODE_PATH Environment Variable
- Set NODE_PATH (Linux/macOS)
- Open your terminal and set the environment variable:
export NODE_PATH=/path/to/your/custom/directory/node_modules -
Replace /path/to/your/custom/directory with your desired path.
-
Make It Permanent (Linux/macOS)
-
Add the above line to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):
echo ‘export NODE_PATH=/path/to/your/custom/directory/node_modules’ >> ~/.bashrc source ~/.bashrc -
Set NODE_PATH (Windows)
- Open System Properties:
- Right-click on This PC or My Computer and select Properties.
- Click Advanced system settings.
- Click Environment Variables.
- Under System variables, scroll to find Path, then click New and add your custom path followed by \node_modules (e.g., C:\path\to\your\custom\directory\node_modules).
Considerations
- Permissions: Ensure the custom directory has appropriate read/write permissions.
- Scope: Decide whether you need the change globally or per user to avoid affecting other users on the same system.
By following these steps, you can customize where npm downloads and installs packages according to your needs.