How to Execute JavaScript in a Jupyter Notebook on Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

Jupyter Notebook is the most popular server-client application that allows you to write, run, test, and debug Python-interpreted programs on notebook documents via a web browser.

Unfortunately, other interpreted programming languages, such as Javascript, lack such fully-fledged applications that offer tight integration and enable users to write programs in notebook documents via a web browser.

Nevertheless, you can leverage the same Jupyter Notebook to execute your JavaScript code by installing a JavaScript runtime environment (such as Node.js) on your system and a Python library (or a kernel) that enables Jupyter to connect to that runtime.

So stick with this article till the end to discover how to execute your JavaScript code in your favorite Jupyter Notebook on Linux.

Setup Jupyter Notebook Environment to Execute Javascript

To achieve JavaScript functionality in Jupyter Notebook, you need to set up an environment that requires multiple steps. However, I’ve provided you with a step-by-step guide, followed by

1. Open your terminal and execute the following command to install Node.js (including NPM) and Python (including Pip) on your Linux system.

$ sudo apt install nodejs npm python3 python3-pip                               #For Debian, Ubuntu, Linux Mint, etc.
$ sudo dnf install nodejs python3 python3-pip                                        #For RHEL, Fedora, Alma Linux, etc.
$ sudo pacman -S nodejs npm python python-pip                                 #For Arch, Manjaro, EndeavourOS, etc.

Output:

installing important tool for running javascript in jupyter

2. Once the installation is finished, verify that each of these has been successfully installed and is accessible from the command-line.

$ nodejs -v
$ npm -v
$ python3 --version
$ pip --version

Output:

checking important tool installed

3. Next, you need to install the Jupyter Notebook by executing the following command:

πŸ“
Sometimes, the installation of Jupyter Notebook is not detected unless a complete system reboot is performed.
$ pip install jupyter

Output:

installing jupyter notebook

4. Now, execute the following command to install IJavascript (a JavaScript kernel for the Jupyter notebook) via NPM.

πŸ’‘
The IJavascript kernel executes JavaScript code within a Node.js session, behaving like the Node.js REPL and granting access to the Node.js standard library and any installed npm modules in Jupyter Notebook.
$ sudo npm install -g ijavascript

Output:

installing ijavascript npm package

5. Finally, execute the β€œijsinstall” command to register the IJavascript kernel with Jupyter, enabling other tools (e.g., the Jupyter notebook) to invoke it.

$ ijsinstall

Output:

executing ijsinstall command

That’s it! Now you can proceed to the next section to learn how to write, run, test, and debug your JavaScript code in Jupyter Notebook (or Jupyter Lab).

How to Execute Javascript on a Jupyter Notebook

After successfully completing all the previously mentioned steps, you can execute the following command to launch Jupyter Lab (or Jupyter Notebook, whichever you prefer).

$ jupyter-lab

#OR

$ jupyter notebook

Output of the β€œJupyter Labβ€œ:

jupyter lab with javascript support

In the β€œNotebook” section, you’ll find the β€œJavaScript (Node.js)” option. Clicking it will promptly launch a new document with the JavaScript kernel, so feel free to proceed.

running javascript code in jupyter lab

Here, you can see the highlighted point β€œ1β€œ, indicating the use of the JavaScript kernel in Jupyter Notebook, with β€œ2” representing our basic JavaScript console code and β€œ3” showing the output of our JavaScript program.

Now, if you wish to work with specific JavaScript libraries, you can do so. For instance, I’ve installed the β€œGet superhero names” library using NPM and randomly generated a hero name.

working with javascript libraries in jupyter lab

Now, I’ll take a pause here, but you can go further and explore more with a complex JavaScript program.

Bonus Tip: Executing JavaScript in Jupyter Notebook using Magic Command

The aforementioned methods require multiple steps, including the installation of a JavaScript runtime and the registration of a JavaScript kernel in your Jupyter notebook.

Nevertheless, all of this can be skipped, allowing you to run basic JavaScript code directly in your Jupyter notebook using a Magic command.

πŸš€
What is Magic Commands in Jupyter Notebook?

In Jupyter Notebooks, β€œmagic commands” are special commands that are not a part of the Python language itself but provide additional functionality and control within the Jupyter environment.

In Jupyter, it starts with either a single β€œ%” character for line magic (applied to a single line) or β€œ%%” for cell magic (applied to the entire cell).

So, to execute the JavaScript in your Jupyter Notebook, you can use either β€œ%%js” or β€œ%%javascript” to instruct the notebook that the specified cell must be treated with the JavaScript kernel.

For example, you can inspect the below screenshot of executing JavaScript in Jupyter Lab (configured with a Python kernel) using the magic command.

running javascript in jupyter using magic command

Keep in mind that this method does not link to any JavaScript runtime, such as Node.js, so you cannot use your NPM-installed packages. It will only run JavaScript code that is compatible with your default browser.

Additionally, this method will treat your current document as an HTML page. Therefore, JavaScript code, such as β€œconsole.log()β€œ, will be displayed in the browser’s console instead of below the cell.

console log javascript script code jupyter notebook magic command

Now, don’t be disheartened, as magic commands also act as the shebang β€œ#!” line of a bash script. So, if you have installed a JavaScript runtime (like Node.js), you can specify β€œ%%script node” to use it and run any JavaScript code in Jupyter notebook that will be processed in Node.js.

running javascript using node.js and magic command in jupyter notebook

With this method, you can also use your favorite NPM-installed packages in your Jupyter notebook (however, it is quite irritating).

working with npm packages in jupyter notebook with magic command

Now, let’s end this article here.

Final Word

In this article, you explored multiple methods for executing your JavaScript code in Jupyter Notebook, with or without external tools.

If you ask for my suggestion, my preference is that if it’s another person’s system, then I use the magic command. Otherwise, I consistently opt for the initial approach of setting up the JavaScript runtime environment and registering the kernel in Jupyter Notebook.

So, I hope you find this article useful. If you have any questions or queries related to the topic, feel free to tell us via the comment section.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.