Skip to content

Instantly share code, notes, and snippets.

@dcfrancisco
Last active April 12, 2024 11:33
Show Gist options
  • Select an option

  • Save dcfrancisco/f45d9d9adccce6276a11fc36d3f7b43f to your computer and use it in GitHub Desktop.

Select an option

Save dcfrancisco/f45d9d9adccce6276a11fc36d3f7b43f to your computer and use it in GitHub Desktop.

Revisions

  1. dcfrancisco renamed this gist Apr 12, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. dcfrancisco renamed this gist Apr 12, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. dcfrancisco created this gist Apr 12, 2024.
    64 changes: 64 additions & 0 deletions gistfile1.txt
    Original 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.