Nextjournal / Dec 20 2019
Node.js Environment
Default environment for Node.js
This notebook describes and creates the default Node.js environment in Nextjournal. Check out the showcase if you want to see what the environment contains. To see how it’s built, see setup.
Nextjournal's environment uses the IJavascript kernel and runs Node.js v12.13.1, with npm v6.12.1 and Yarn v1.21.1 for package management.
Showcase
function Person(name) { this.name = name;}var peter = new Person("Peter");var util = require("util");console.log(util.inspect(peter)); // shown on stdoutpeter; // shown as an execution resultPerson.prototype.inspect = function inspect(depth) { return "Person named " + this.name;};peter;Person.prototype._toHtml = function _toHtml() { var style = ( "display:inline-block;" + "padding:0.25em;" + "background:#ccc;" + "border: 1px solid #888;" + "border-radius:0.25em;" + "box-shadow: 0.8em 0.4em 0.4em black;" ); return ( "<div style='" + style + "'>" + this.inspect() + "</div>" );};peter;1.3s
Node Test (Node.js)
Node.js
Person named Peter
Setup
Build the node environment
Download Node.js and Yarn binaries.
FILENAME="node-v${NODEJS_VERSION}-linux-x64.tar.xz"FILEURL="https://nodejs.org/dist/v${NODEJS_VERSION}/${FILENAME}"curl -O https://nodejs.org/dist/v${NODEJS_VERSION}/SHASUMS256.txtwget --progress=dot:giga ${FILEURL}grep $FILENAME SHASUMS256.txt | sha256sum -c -cp $FILENAME /results/FILENAME="yarn-v${YARN_VERSION}.tar.gz"FILEURL="https://github.com/yarnpkg/yarn/releases/download/v${YARN_VERSION}/${FILENAME}"wget -qO- https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --importwget --progress=dot:giga ${FILEURL} ${FILEURL}.ascgpg --verify ${FILENAME}.asccp $FILENAME /results/3.5s
Node.js (Bash)
Install Node.js.
mkdir -p /opt/nodejscd /opt/nodejstar -Jxf NJ__REF_ln -s node-v${NODEJS_VERSION}-linux-x64/ default2.7s
Node.js (Bash)
Install Yarn.
cd /opttar -zxf NJ__REF_ln -s yarn-${YARN_VERSION} yarn0.6s
Node.js (Bash)
Configure npm.
npm config set python /usr/bin/python3npm config set unsafe-perm true # required because we are root0.8s
Node.js (Bash)
Install IJavascript.
npm install -g ijavascriptijsinstall --install=global7.3s
Node.js (Bash)
Rename kernel so it doesn't conflict with browser-side JS cells.
cd /usr/local/share/jupyter/kernelsmv javascript nodejscd nodejssed -i 's/"display_name":.*Javascript (Node.js)"/"display_name":"Node.js"/s/"language":.*javascript"/"language":"nodejs"/' kernel.json0.1s
Node.js (Bash)
Get some other packages.
npm install -g plotly.jsnpm install -g plotta.js34.2s
Node.js (Bash)
Check.
du -hsx /npm versionnode -vjupyter kernelspec list2.4s
Node.js (Bash)
Test
"use strict";$$.async();{ let console = global.console; let $$ = global.$$; setTimeout(function() { console.log("Hello, world!"); $$.sendResult("Bye!"); }, 1000);}1.4s
Node Test (Node.js)
Node.js
'Bye!'