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 NODEJS_VER=${WEBI_VERSION:-}
55 NODEJS_VER="${NODEJS_VER:-v}" # Search for 'v' at the least
57 # WEBI_ARCH uses only slightly different names from NODE_ARCH
58 NODE_OS="${WEBI_OS}" # linux or darwin
59 NODE_ARCH="${WEBI_ARCH}"
60 if [ "amd64" == "$NODE_ARCH" ]; then
72 # sort -rV # will sort by version number, but it appears these are already sorted
73 # tail -n +2 # starts at line two (1-indexed) and all after (omits the csv header with 'version' and such)
74 # cut -f 1 # gets only the first column
75 # head -n 1 # gets only the most recent version
77 my_count=$(awk -F"${my_char}" '{print NF-1}' <<< "${NODEJS_VER}")
78 # get the latest version if partial
79 if [ $my_count -ne 2 ]; then
80 if [ "$NODEJS_VER" != "v" ]; then
81 NODEJS_VER="$NODEJS_VER\\."
83 if [ -n "$(type -p curl)" ]; then
84 NODEJS_VER=$(curl -fsL "https://nodejs.org/dist/index.tab" | tail -n +2 | cut -f 1 | grep "^$NODEJS_VER" | head -n 1) \
85 || echo 'error automatically determining current node.js version'
86 elif [ -n "$(type -p wget)" ]; then
87 NODEJS_VER=$(wget --quiet "https://nodejs.org/dist/index.tab" -O - | tail -n +2 | cut -f 1 | grep "^$NODEJS_VER" | head -n 1) \
88 || echo 'error automatically determining current node.js version'
90 echo "Found neither 'curl' nor 'wget'. Can't Continue."
101 node_install_path=$HOME/.local/opt/node-${NODEJS_VER}
102 mkdir -p $node_install_path
104 # TODO warn if existing node in path my take precedence
105 if [ -e "$node_install_path/bin/node" ]; then
106 # node of some version is already installed
107 if [ "${NODEJS_VER}" == "$($node_install_path/bin/node -v 2>/dev/null)" ]; then
108 echo node ${NODEJS_VER} already installed at $node_install_path
113 NODEJS_REMOTE="https://nodejs.org/dist/${NODEJS_VER}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}.tar.gz"
114 NODEJS_LOCAL="$my_tmp/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}.tar.gz"
115 NODEJS_UNTAR="$my_tmp/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}"
117 echo "installing node as node ${NODEJS_VER}..."
119 if [ -n "$(command -v curl 2>/dev/null | grep curl)" ]; then
120 curl -fsSL ${NODEJS_REMOTE} -o ${NODEJS_LOCAL} || echo 'error downloading node'
121 elif [ -n "$(command -v wget 2>/dev/null | grep wget)" ]; then
122 wget --quiet ${NODEJS_REMOTE} -O ${NODEJS_LOCAL} || echo 'error downloading node'
124 echo "'wget' and 'curl' are missing. Please run the following command and try again"
125 echo " sudo apt-get install --yes curl wget"
129 mkdir -p ${NODEJS_UNTAR}/
130 # --strip-components isn't portable, switch to portable version by performing move step after untar
131 tar xf ${NODEJS_LOCAL} -C ${NODEJS_UNTAR}/ #--strip-components=1
132 mv ${NODEJS_UNTAR}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH}/* ${NODEJS_UNTAR}/
133 rm -rf ${NODEJS_UNTAR}/node-${NODEJS_VER}-${NODE_OS}-${NODE_ARCH} # clean up the temporary unzip folder
134 rm ${NODEJS_UNTAR}/{LICENSE,CHANGELOG.md,README.md}
135 if [ -n "$(command -v rsync 2>/dev/null | grep rsync)" ]; then
136 echo $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
137 rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/" 2>/dev/null || $sudo_cmd rsync -Krl "${NODEJS_UNTAR}/" "$node_install_path/"
139 # due to symlink issues on Arch Linux, don't copy the share directory
140 rm -rf ${NODEJS_UNTAR}/share
141 echo $sudo_cmd cp -Hr "${NODEJS_UNTAR}/*" "$node_install_path/"
142 cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/" 2>/dev/null || $sudo_cmd cp -Hr "${NODEJS_UNTAR}"/* "$node_install_path/"
145 rm -rf "${NODEJS_UNTAR}"
147 chown -R $(whoami) "$node_install_path/lib/node_modules/" 2>/dev/null || $sudo_cmd chown -R $(whoami) "$node_install_path/lib/node_modules/"
148 chown $(whoami) "$node_install_path"/bin/ 2>/dev/null || $sudo_cmd chown $(whoami) "$node_install_path"/bin/
150 mkdir -p $node_install_path/lib/node_modules 2> /dev/null || $sudo_cmd mkdir -p $node_install_path/lib/node_modules
151 chown -R $(whoami) $node_install_path/lib/node_modules 2> /dev/null || $sudo_cmd chown -R $(whoami) $node_install_path/lib/node_modules
153 # By default, npm is stupid and uses any version of node in any path. Stop that.
154 # npm config set scripts-prepend-node-path true
155 "$node_install_path"/bin/node "$node_install_path"/bin/npm --scripts-prepend-node-path=true config set scripts-prepend-node-path true
161 pathman add $node_install_path/bin