DEV Community

Cover image for A Guide to Node.js Notebooks
raphiki for Technology at Worldline

Posted on • Edited on

A Guide to Node.js Notebooks

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

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 - 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 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 β€” 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

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:



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


Enter fullscreen mode Exit fullscreen mode

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

A peek into Anaconda Navigator

Crafting our first Nodebook

Initiate Jupyter via the Anaconda interface!

Launching Jupyter Notebook

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

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

Exploring the Node.js Notebook

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.

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



npm install langchain
npm install esm-hook


Enter fullscreen mode Exit fullscreen mode

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

Nodebook tailored for OpenAI GPT

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!

Top comments (0)