4 # homepage: https://nodejs.org
5 # tagline: JavaScript V8 runtime
7 # Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine
10 # node -e 'console.log("Hello, World!")'
18 # <td>Run a webserver</td>
19 # <td><pre><code class="language-bash">
23 # npm install --save express</code></pre>
25 # <code>app.js:</code>
27 # <pre><code class="language-javascript">'use strict'
28 # var express = require('express');
29 # var app = express();
31 # app.use('/', function (req, res, next) {
32 # res.end("Hello, World!");
35 # module.exports = app;</code></pre>
37 # <code>server.js:</code>
39 # <pre><code class="language-javascript">'use strict'
40 # var http = require('http');
41 # var app = require('./app.js');
42 # http.createServer(app).listen(8080, function () {
43 # console.log('Listening on', this.address());
46 # <pre><code class="language-bash">npm start</code></pre>
54 my_tmp=${WEBI_TMP:-$(mktemp -d node-install.XXXXXX)}
55 sudo_cmd=${WEBI_SUDO:-}
58 if [ -n "$(command -v curl)" ]; then
59 curl -fsSL $1 -o $2 || echo 'error downloading node'
60 elif [ -n "$(command -v wget)" ]; then
61 wget --quiet $1 -O $2 || echo 'error downloading node'
63 echo "'wget' and 'curl' are missing. Please run the following command and try again"
65 echo " sudo apt-get install --yes curl wget"
70 WEBI_CSV=$(curl -fsSL "https://webinstall.dev/api/releases/node@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1" -H "User-Agent: $(uname -a)")
71 NODEJS_VER=$(echo $WEBI_CSV | cut -d ',' -f 1)
72 NODEJS_REMOTE=$(echo $WEBI_CSV | cut -d ',' -f 9)
73 NODEJS_LOCAL="$my_tmp/$(echo $NODEJS_REMOTE | sed s:.*/::)"
74 NODE_OS="$(echo $WEBI_CSV | cut -d ',' -f 5)"
80 # WEBI_ARCH uses only slightly different names from NODE_ARCH
81 NODE_OS="$(echo $WEBI_CSV | cut -d ',' -f 5)"
82 if [ "macos" == "$NODE_OS" ]; then
85 NODE_ARCH="$(echo $WEBI_CSV | cut -d ',' -f 6)"
86 if [ "amd64" == "$NODE_ARCH" ]; then
90 node_install_path=$HOME/.local/opt/node-v${NODEJS_VER}
91 mkdir -p $node_install_path
93 if [ -e "$node_install_path/bin/node" ]; then
94 # node of some version is already installed
95 if [ "v${NODEJS_VER}" == "$($node_install_path/bin/node -v 2>/dev/null)" ]; then
96 echo node ${NODEJS_VER} already installed at $node_install_path
101 # TODO warn if existing node in path my take precedence
103 echo "downloading node v${NODEJS_VER}..."
104 http_get ${NODEJS_REMOTE} ${NODEJS_LOCAL} || echo 'error downloading node'
106 echo "installing node v${NODEJS_VER}..."
107 tar xf ${NODEJS_LOCAL} -C $my_tmp/
108 # we know how it'll unpack
109 NODEJS_UNTAR=$my_tmp/node-v${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}
111 # this funny business is to allow something a non-/opt directory
112 # ( such as /usr/local ) to be an install target
113 rm ${NODEJS_UNTAR}/{LICENSE,CHANGELOG.md,README.md}
114 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
115 echo $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
116 rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
118 # due to symlink issues on Arch Linux, don't copy the share directory
119 rm -rf ${NODEJS_UNTAR}/share
120 echo $sudo_cmd cp -Hr "${NODEJS_UNTAR}/*" "$node_install_path/"
121 cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/"
123 rm -rf "${NODEJS_UNTAR}"
126 # By default, npm is stupid and uses any version of node in any path. Stop that.
127 # npm config set scripts-prepend-node-path true
128 "$node_install_path"/bin/node "$node_install_path"/bin/npm --scripts-prepend-node-path=true config set scripts-prepend-node-path true
134 pathman add $node_install_path/bin