3 # A word about this shell script:
5 # It must work everywhere, including on systems that lack
6 # a /bin/bash, map 'sh' to ksh, ksh97, bash, ash, or zsh,
7 # and potentially have either a posix shell or bourne
8 # shell living at /bin/sh.
10 # See this helpful document on writing portable shell scripts:
11 # http://www.gnu.org/s/hello/manual/autoconf/Portable-Shell.html
13 # The only shell it won't ever work on is cmd.exe.
15 if [ "x$0" = "xsh" ]; then
17 # on some systems, you can just do cat>npm-install.sh
18 # which is a bit cuter. But on others, &1 is already closed,
19 # so catting to another script file won't do anything.
20 # Follow Location: headers, and fail on errors
21 curl -f -L -s https://www.npmjs.org/install.sh > npm-install-$$.sh
23 if [ $ret -eq 0 ]; then
27 echo "Failed to download script" >&2
36 # See what "npm_config_*" things there are in the env,
37 # and make them permanent.
38 # If this fails, it's not such a big deal.
39 configures="`env | grep 'npm_config_' | sed -e 's|^npm_config_||g'`"
41 npm_config_loglevel="error"
42 if [ "x$npm_debug" = "x" ]; then
45 echo "Running in debug mode."
46 echo "Note that this requires bash or zsh."
49 npm_config_loglevel="verbose"
51 export npm_config_loglevel
53 # make sure that node exists
54 node=`which node 2>&1`
56 if [ $ret -eq 0 ] && [ -x "$node" ]; then
59 echo "npm cannot be installed without node.js." >&2
60 echo "Install node first, and then try again." >&2
62 echo "Maybe node is installed, but not in the PATH?" >&2
63 echo "Note that running as sudo can change envs." >&2
71 if [ "x$TMP" = "x" ]; then
78 echo "failed to mkdir $TMP" >&2
86 if [ -z "$tar" ]; then
87 tar="${npm_config_tar}"
89 if [ -z "$tar" ]; then
94 if [ $ret -eq 0 ] && [ -x "$tar" ]; then
101 if [ $ret -eq 0 ]; then
104 echo "No suitable tar program found."
110 # Try to find a suitable make
111 # If the MAKE environment var is set, use that.
112 # otherwise, try to find gmake, and then make.
113 # If no make is found, then just execute the necessary commands.
115 # XXX For some reason, make is building all the docs every time. This
116 # is an annoying source of bugs. Figure out why this happens.
119 if [ "x$MAKE" = "x" ]; then
120 make=`which gmake 2>&1`
121 if [ $? -eq 0 ] && [ -x "$make" ]; then
124 make=`which make 2>&1`
125 if [ $? -eq 0 ] && [ -x "$make" ]; then
135 if [ -x "$make" ]; then
138 # echo "Installing without make. This may fail." >&2
142 # If there's no bash, then don't even try to clean
143 if [ -x "/bin/bash" ]; then
149 node_version=`"$node" --version 2>&1`
151 if [ $ret -ne 0 ]; then
152 echo "You need node to run this program." >&2
153 echo "node --version reports: $node_version" >&2
154 echo "with exit code = $ret" >&2
155 echo "Please install node before continuing." >&2
161 # switch based on node version.
162 # note that we can only use strict sh-compatible patterns here.
163 case $node_version in
164 0.[01234567].* | v0.[01234567].*)
165 echo "You are using an outdated and unsupported version of" >&2
166 echo "node ($node_version). Please update node and try again." >&2
170 echo "install npm@latest"
176 # need to echo "" after, because Posix sed doesn't treat EOF
177 # as an implied end of line.
178 url=`(curl -SsL https://registry.npmjs.org/npm/$t; echo "") \
179 | sed -e 's/^.*tarball":"//' \
183 if [ "x$url" = "x" ]; then
185 # try without the -e arg to sed.
186 url=`(curl -SsL https://registry.npmjs.org/npm/$t; echo "") \
187 | sed 's/^.*tarball":"//' \
190 if [ "x$url" = "x" ]; then
194 if [ $ret -ne 0 ]; then
195 echo "Failed to get tarball url for npm/$t" >&2
200 echo "fetching: $url" >&2
203 && curl -SsL "$url" \
206 && (ver=`"$node" bin/read-package-json.js package.json version`
208 if [ $ret -eq 0 ]; then
209 if [ -d node_modules ]; then
210 if "$node" node_modules/semver/bin/semver -v "$ver" -r "1"
215 if "$node" bin/semver -v "$ver" -r ">=1.0"; then
222 if [ $isnpm10 -eq 1 ] && [ -f "scripts/clean-old.sh" ]; then
223 if [ "x$skipclean" = "x" ]; then
228 if [ "x$clean" = "xno" ] \
229 || [ "x$clean" = "xn" ]; then
230 echo "Skipping 0.x cruft clean" >&2
232 elif [ "x$clean" = "xy" ] || [ "x$clean" = "xyes" ]; then
233 NODE="$node" /bin/bash "scripts/clean-old.sh" "-y"
236 NODE="$node" /bin/bash "scripts/clean-old.sh" </dev/tty
241 if [ $ret -ne 0 ]; then
242 echo "Aborted 0.x cleanup. Exiting." >&2
245 && (if [ "x$configures" = "x" ]; then
248 echo "./configure $configures"
249 echo "$configures" > npmrc
251 && (if [ "$make" = "NOMAKE" ]; then
253 elif "$make" uninstall install; then
258 if [ "$make" = "NOMAKE" ]; then
259 "$node" cli.js rm npm -gf
260 "$node" cli.js install -gf
267 if [ $ret -ne 0 ]; then