A Guide to Node.js Notebooks
Tags: jupyter, notebooks, javascript, node
Published: 2023-08-11 14:34:16

{% youtube la928ga1AbQ %}
## Nodebooks: the evolution of Notebooks in JavaScript

The increasing popularity of machine learning and AI, largely fueled by the Python ecosystem, brought about the concept of computation notebooks. These interactive computational platforms, primarily developed for Python-centric data science applications, merge code, computational output, explanatory text, and multimedia into one cohesive document.

![A glimpse of Jupyter Notebook with a JS kernel](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dpms0mfs3vj0vhu5yec8.png)

As an enthusiast of JavaScript, I've always wanted to harness the potential of this environment for Node.js. To achieve this, I selected [Jupyter Notebook](https://en.wikipedia.org/wiki/Project_Jupyter) - a leading open-source alternative. It enables in-browser code editing and execution, presenting computational outcomes. Moreover, it's adaptable, supporting an array of languages, inclusive of a [Javascript](https://github.com/n-riesco/ijavascript) command shell.

The IJavascript kernel executes Javascript within a Node.js session, allowing access to the Node.js standard library as well as any installed npm modules.

## Setting Up Jupyter Notebook on Windows

Ready to venture into setting up Jupyter Notebook on Windows?

Firstly, Jupyter Notebook relies on Python, necessitating a Python distribution for Windows. I've chosen [Anaconda](https://www.anaconda.com) — a comprehensive Python distribution tailored for big data, AI, and machine learning. It's always a good choice, especially if I ever lean towards exploring Python more deeply. Notably, Anaconda's package manager, conda, has a more reliable dependency management system than pip. Plus, it offers a graphical interface and pre-packaged tools, including Jupyter Notebook.

![Command Prompt in action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8ki1dfmrknvs6u5mbou.png)

Post installation, the Anaconda Navigator presents a user-friendly interface. Here’s how to set up Jupyter, Node.js, and IJavascript using a Windows command prompt within an activated Anaconda environment:

```shell
conda install -c conda-forge jupyter
conda install nodejs
npm install -g ijavascript
ijsinstall
```

After a restart, Jupyter Notebook becomes accessible via the Anaconda Navigator.

![A peek into Anaconda Navigator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eops854ifaq1wvibn7yb.png)

## Crafting our first Nodebook

Initiate Jupyter via the Anaconda interface!

![Launching Jupyter Notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ut8mu494w42s1e7thypk.png)

Launching it directs you to a new browser tab with the address http://localhost:8888. From the upper right menu, you can easily craft a new Notebook using the Node.js kernel.

![Creating a new Notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1gov1vee5frhvh8dvv0.png)

Once opened in a fresh tab, you're set! Document and interact within a Node.js environment concurrently.

![Exploring the Node.js Notebook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42vrwfm4fje7u7quuy6o.png)

## Experimenting with Node.js

Let's delve deeper and install the LangChain framework in its JavaScript rendition. Following this, we'll make an API call to OpenAI GPT Completion, as outlined in [one of my previous articles](https://dev.to/worldlinetech/in-bed-with-gpt-and-nodejs-4kh2).

For efficient ES Modules use within the IJavascript kernel, the esm-hook package comes in handy. To install these packages, use the Command Prompt:

```shell
npm install langchain
npm install esm-hook
```

Now, it's time to craft a new Nodebook and immerse in both documentation and coding.

![Nodebook tailored for OpenAI GPT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdw0db4g8x7l7g0g56w3.png)

Some key takeaways:
- Notice the Node.js kernel indication on the top right.
- The initial two blocks are structured in Markdown (addressing title and prerequisites).
- The concluding two blocks represent JavaScript codes:
  - The primary block incorporates module imports and declares our function for the OpenAI API call.
  - The subsequent block initiates this function. Note its asynchronous nature, marked by the `Promise { <pending> }` shown by Jupyter prior to unveiling the final GPT outcome.

That wraps it up for now. Dive in and enjoy your Nodebooking journey!