Getting Started in Node.js with Conda

Lois T.
3 min readAug 3, 2023

Using Anaconda for virtual environment control

A minimal Hello World node.js project (image by author)

Introduction

When working with multiple Node.js projects, I’ve encountered this problem more often than I’d liked — the project is running on v14+, but my machine is running v18. Anaconda can provide a convenient solution as it lets us create multiple virtual environments that can be switched easily.

About Anaconda

Anaconda is a software for environment control and commonly used in AI/ML. While I’ve used it for Python in AI/ML projects, I’m happy to find out that it works for Node.js too. Anaconda can be downloaded here — this version comes with a full desktop graphical application and might be more than necessary for just web development. There is a much smaller CLI-only version called miniconda, and it can be downloaded here.

Once installed, we can use conda/miniconda through the CLI (Terminal in macs or the Miniconda Prompt in windows). The word (base) should appear right on the front/left of the prompt.

Working with the environments

In the anaconda/minconda CLI:

Create a new virtual environment

conda create -n node18 python=3.9

This will create an environment called node18 with python v3.9 installed.

Check current list of all environments

conda env list

The environments base and node18 should be printed, with an asterisk (*) beside base, indicating that it is the current active environment.

Switch environments

conda activate node18

This switches the current active environment to the newly created node18. We can verify that easily by noticing the front of the prompt. It should have changed from the (base) earlier to (node18).

Installing node.js

In the new node18 environment:

Install node.js

conda config --add channels conda-forge
conda install nodejs

Verify installation

node --version
npm --version

Creating a Hello World web page with Express

With the environment properly set up now, we can try it out by building a minimal Hello World project with Express.

Assuming a new folder has been created in e.g. Documents/HelloWorld:

Create the file, app.js

import express from 'express';
var app = express();
app.use(express.static('public'));
app.listen(3000, () => {
console.log("Server running on port 3000");
});
app.get("/", (req, res) => {
res.send('Hello World!')
});

Initialise the node.js project

Navigate to the folder (Documents/HelloWorld) through the CLI and enter:

npm init

Press Enter to step through all the steps and enter yes for the final prompt. A file package.json will be created.

Additional Settings

If we try to run the project now, an error “Cannot use import statement outside a module” will be shown. (This is because I’m using import instead of require, but that’s for another article.) Open the package.json file and add the following line:

"type": "module"

The whole package.json file should look something like this now:

{
"name": "helloworld",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"type": "module"
}

Install Dependencies

The project requires the Express module in order to render the web page, and we can install this using the Node Package Manager, npm.

npm install express

Run the program

node app.js   

To verify it, open localhost:3000 on your browser.

You should see Hello World! printed on your screen.

Stopping the program

You can stop the application by entering Ctrl+C.

--

--

Lois T.

I make web-based systems and recently AI/ML. I write about the dev problems I meet in a simplified manner (explain-like-I’m-5) because that’s how I learn.