Last active
April 12, 2024 11:33
-
-
Save dcfrancisco/f45d9d9adccce6276a11fc36d3f7b43f to your computer and use it in GitHub Desktop.
Revisions
-
dcfrancisco renamed this gist
Apr 12, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dcfrancisco renamed this gist
Apr 12, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
dcfrancisco created this gist
Apr 12, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,64 @@ Moving the global installation location for Node.js packages to a directory in your home folder on a MacBook can be a good way to avoid having to use `sudo` for npm installs. This allows you to manage global packages without needing administrative privileges. Here’s a step-by-step guide to achieve this: ### 1. Create a New Directory for Global Packages Open your terminal and run the following command to create a new directory where your global npm packages will be stored: ```bash mkdir ~/.npm-global ``` ### 2. Configure npm to Use the New Directory Configure npm to use the new directory for global packages. This can be done by setting the `prefix` configuration parameter: ```bash npm config set prefix '~/.npm-global' ``` ### 3. Update Your PATH For your system to recognize and run the packages installed in the new directory, you need to add it to your PATH environment variable. You can add the following line to your `.bash_profile`, `.zshrc`, or equivalent file depending on which shell you use: ```bash export PATH=~/.npm-global/bin:$PATH ``` To do this using a command in the terminal: ```bash echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bash_profile ``` Or if you are using zsh (as is default in some recent versions of macOS): ```bash echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc ``` Then, apply the changes: ```bash source ~/.bash_profile # or ~/.zshrc if using zsh ``` ### 4. Test Your Setup To ensure that everything is set up correctly, try installing a package globally without `sudo`. For example: ```bash npm install -g jshint ``` If there are no errors and the command completes successfully, then your new setup is working correctly. ### 5. Optionally, Check npm Variables If you want to verify that npm is indeed using the new directory, you can check the npm configuration: ```bash npm config get prefix # Should return ~/.npm-global ``` You can also verify that the PATH is set correctly: ```bash echo $PATH # Should include ~/.npm-global/bin ``` By following these steps, you can install global npm packages on your MacBook without needing to use `sudo`, which enhances your security and simplifies package management.