title: npm install not installing recursive dependencies on Ubuntu due to permissions
body: <h2>Problem Description</h2>
  
  <p>When installing <code>npm</code> packages on a Ubuntu server from <code>package.json</code> with <code>sudo npm install</code>, many dependencies are not installed. Running <code>npm install</code> locally installs everything as expected. The problem appears to be caused by permissions issues inside the <code>node_modules/</code> directory. How can I ensure that <code>npm</code> is able to install all dependencies recursively?</p>
  
  <h2>Background</h2>
  
  <p>I'm deploying a NodeJS application to a Ubuntu webserver. All of my packages are defined in my <code>package.json</code> file, something like this:</p>
  
  <pre><code>{
    "name": "MyApp",
    "engines": {
      "node": "0.10.x",
      "npm": "1.2.x"
    },
    "dependencies": {
      "express": "~3.5.1",
      "jade": "~1.3.1",
      "mongoose": "3.8.8",
      ...
    }
  }
  </code></pre>
  
  <p>When I run <code>npm install</code> locally, I'm able to immediately spin up the node server with <code>node server.js</code> without any problems (after running all tasks/pre-compilation jobs).</p>
  
  <p>Running <code>npm install</code> on the server fails, due to <code>EACCES</code> errors like this one:</p>
  
  <pre><code>npm ERR! error rolling back Error: EACCES, unlink '/myapp/staging/node_modules/.bin/express'
  npm ERR! error rolling back  express@3.5.1 { [Error: EACCES, unlink '/myapp/staging/node_modules/.bin/express']
  npm ERR! error rolling back   errno: 3,
  npm ERR! error rolling back   code: 'EACCES',
  npm ERR! error rolling back   path: '/myapp/staging/node_modules/.bin/express' }
  npm ERR! Error: EACCES, unlink '/myapp/staging/node_modules/.bin/express'
  npm ERR!  { [Error: EACCES, unlink '/myapp/staging/node_modules/.bin/express']
  npm ERR!   errno: 3,
  npm ERR!   code: 'EACCES',
  npm ERR!   path: '/myapp/staging/node_modules/.bin/express' }
  npm ERR! 
  npm ERR! Please try running this command again as root/Administrator.
  </code></pre>
  
  <p>When I run <code>sudo npm install</code> on the server (after removing <code>node_modules/</code> and running <code>sudo npm cache clean</code>), <code>npm</code> fetches all the top-level packages:</p>
  
  <pre><code>npm http GET https://registry.npmjs.org/jade
  npm http GET https://registry.npmjs.org/mongoose
  npm http GET https://registry.npmjs.org/express
  ...
  </code></pre>
  
  <p>but doesn't use <code>sudo</code> when installing the dependencies, so they quietly fail without any error message. The result is that the server fails to start:</p>
  
  <pre><code>$ node server.js
  
  module.js:340
    throw err;
        ^
  Error: Cannot find module './lib/express'
      at Function.Module._resolveFilename (module.js:338:15)
      at Function.Module._load (module.js:280:25)
      at Module.require (module.js:364:17)
      at require (module.js:380:17)
      at Object.&lt;anonymous&gt; (/myapp/node_modules/express/index.js:4:5)
      at Module._compile (module.js:456:26)
      at Object.Module._extensions..js (module.js:474:10)
      at Module.load (module.js:356:32)
      at Function.Module._load (module.js:312:12)
      at Module.require (module.js:364:17)
  </code></pre>
  
  <p>and the solution is to install each top-level package individually, which then properly fetches and installs the recursive dependencies:</p>
  
  <pre><code>$ sudo npm install express
  ...
  </code></pre>
  
  <p>However that defeats the entire purpose of <code>package.json</code>, not to mention requires heavy maintenance for every new deployment.</p>