title: How to upgrade npm inside a docker container?
body: <p>I use the image <code>node:6.9</code> and I try to update to the version of npm from v3 to v4.</p>
  
  <p>My Dockerfile looks like:</p>
  
  <pre><code>FROM node:6.9
  MAINTAINER Philipp Kretzschmar &lt;philipp.kretzschmar@gmail.com&gt;
  ENV TERM xterm-color
  ARG DEBIAN_FRONTEND=noninteractive
  COPY docker/.docker-bashrc /root/.bashrc
  RUN apt-get -qq update &gt; /dev/null &amp;&amp; \
      apt-get upgrade -qq &gt; /dev/null &amp;&amp; \
      apt-get install -qq rsync silversearcher-ag jq httpie vim s3cmd &gt; /dev/null &amp;&amp; \
      apt-get autoclean -qq
  RUN npm update -g # this should upgrade npm!
  RUN npm install typescript@2 -g --quiet &gt; /dev/null
  </code></pre>
  
  <p>Yet it fails when running </p>
  
  <pre><code>npm update -g
  </code></pre>
  
  <p>stating:</p>
  
  <pre><code>npm ERR! Linux 4.4.0-51-generic
  npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "update" "npm" "-g"
  npm ERR! node v6.9.1
  npm ERR! npm  v3.10.8
  npm ERR! path /usr/local/lib/node_modules/npm/node_modules/asap
  npm ERR! code EXDEV
  npm ERR! errno -18
  npm ERR! syscall rename
  
  npm ERR! EXDEV: cross-device link not permitted, rename '/usr/local/lib/node_modules/npm/node_modules/asap' -&gt; '/usr/local/lib/node_modules/npm/node_modules/.asap.DELETE'
  </code></pre>
  
  <p>There's an <a href="https://github.com/npm/npm/issues/9863" rel="nofollow noreferrer">open github issue</a> on npm already, and there the workaround is provided to use the <code>install.sh</code> script:</p>
  
  <pre><code>ADD https://www.npmjs.com/install.sh ./install.sh
  RUN sh install.sh
  </code></pre>
  
  <p>which seems to download the npm 4.0.3 and and install it, yet now npm cannot be started from inside the container stating:</p>
  
  <pre><code>$ npm
  module.js:471
      throw err;
      ^
  
  Error: Cannot find module 'npmlog'
      at Function.Module._resolveFilename (module.js:469:15)
      at Function.Module._load (module.js:417:25)
      at Module.require (module.js:497:17)
      at require (internal/module.js:20:19)
      at /usr/local/lib/node_modules/npm/bin/npm-cli.js:19:13
      at Object.&lt;anonymous&gt; (/usr/local/lib/node_modules/npm/bin/npm-cli.js:75:3)
      at Module._compile (module.js:570:32)
      at Object.Module._extensions..js (module.js:579:10)
      at Module.load (module.js:487:32)
      at tryModuleLoad (module.js:446:12)
  </code></pre>
  
  <p>What workaround that npm bug and and update npm version to a later version for a docker image?</p>