<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hamza Makraz</title>
    <description>The latest articles on DEV Community by Hamza Makraz (@makraz).</description>
    <link>https://dev.to/makraz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F266502%2F369a92f2-ffe6-44e3-941e-bdb36bcd7fa2.png</url>
      <title>DEV Community: Hamza Makraz</title>
      <link>https://dev.to/makraz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/makraz"/>
    <language>en</language>
    <item>
      <title>Install packages globally without sudo on Linux / MacOS</title>
      <dc:creator>Hamza Makraz</dc:creator>
      <pubDate>Sun, 10 May 2020 23:40:55 +0000</pubDate>
      <link>https://dev.to/makraz/install-packages-globally-without-sudo-on-linux-macos-4mgn</link>
      <guid>https://dev.to/makraz/install-packages-globally-without-sudo-on-linux-macos-4mgn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Originally published at &lt;a href="https://www.makraz.com/posts/npm-global-without-sudo/" rel="noopener noreferrer"&gt;makraz.com&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  &lt;code&gt;npm&lt;/code&gt;  -- Install packages globally without sudo on Linux / MacOS
&lt;/h1&gt;

&lt;p&gt;By default &lt;code&gt;npm&lt;/code&gt; installs packages locally in the folder nodes_modules within your projects.&lt;br&gt;
But sometimes with need to install packages globally for command-line as example. &lt;br&gt;
For Linux / MacOS users npm will ask you to run it with elevated privileges,&lt;br&gt;
which mean use the command &lt;code&gt;sudo&lt;/code&gt; to be able to install globally (&lt;code&gt;sudo npm install -g &amp;lt;package&amp;gt;&lt;/code&gt;) &lt;br&gt;
and this may create permission(s) issue(s) for many users.&lt;br&gt;
However there is a way to install packages globally for a given user without &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For that we will follow these steps:  &lt;/p&gt;
&lt;h6&gt;
  
  
  1. Create a directory for global packages
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="s2"&gt;"~/.npm_packages"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h6&gt;
  
  
  2. Tell &lt;code&gt;npm&lt;/code&gt; where to store globally installed packages
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm config &lt;span class="nb"&gt;set &lt;/span&gt;prefix &lt;span class="s2"&gt;"~/.npm_packages"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h6&gt;
  
  
  3. Ensure &lt;code&gt;npm&lt;/code&gt; will find installed binaries and man pages
&lt;/h6&gt;

&lt;p&gt;Add the following to your &lt;code&gt;.bashrc&lt;/code&gt; / &lt;code&gt;.zshrc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;NPM_PACKAGES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"~/.npm_packages"&lt;/span&gt;

&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$NPM_PACKAGES&lt;/span&gt;&lt;span class="s2"&gt;/bin"&lt;/span&gt;

&lt;span class="c"&gt;# Preserve MANPATH if you already defined it somewhere in your config.&lt;/span&gt;
&lt;span class="c"&gt;# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.&lt;/span&gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;MANPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;MANPATH&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;manpath&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="nv"&gt;$NPM_PACKAGES&lt;/span&gt;&lt;span class="s2"&gt;/share/man"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;NOTE: If you are running macOS, the &lt;code&gt;.bashrc&lt;/code&gt; file may not yet exist, and the terminal will be obtaining its environment parameters from another file, such as &lt;code&gt;.profile&lt;/code&gt; or &lt;code&gt;.bash_profile&lt;/code&gt;. These files also reside in the user's home folder. In this case, simply adding the following line to them will instruct Terminal to also load the &lt;code&gt;.bashrc&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;See also: &lt;code&gt;npm&lt;/code&gt;'s documentation on &lt;a href="https://docs.npmjs.com/getting-started/fixing-npm-permissions" rel="noopener noreferrer"&gt;"Fixing &lt;code&gt;npm&lt;/code&gt; permissions"&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>npm</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
