Using libraries
Last updated on
Millions of people around the world work with JavaScript and TypeScript. Part of the reason for their popularity is the vast ecosystem of libraries. Many developers share their code publicly, making it free for anyone to use.
In later lessons, we will use open-source libraries to analyze huge datasets and create stunning visualizations.
A note about vocabulary: libraries are also often called packages or modules.
I assume you’ve completed Chapter 1 and all previous lessons in Chapter 2 before diving into this one. In particular, make sure you’ve followed the Setup lesson.
Setup
Create a new, empty folder on your computer, open it with VS Code, and create a file named main.ts inside it.
Type this code in main.ts and save the file:
console.log("Hello!");Open the terminal (Command + J on Mac or Control + J on Windows) and run this command:
node --watch main.tsThis runs our code and automatically runs it again each time we save a change, so we can see the results as we work.

The npm registry
Libraries are published on public registries. In this course, we’ll install them from npm , the registry used by the npm command that comes with Node.js. Package pages include documentation, maintainers, versions, and download counts to help you choose a library.
As an example, let’s search for d3, a well-known library for data visualization that we will use frequently.
Go to npmjs.com and search for d3.

Select the first result: d3 with the description “Data-Driven Documents” by Mike Bostock.
Here’s the direct link , just in case.

This page provides a lot of useful information:
- In the main section, we see a short description and links to documentation, examples, and more.
- On the right-hand side, there’s a link to the GitHub repository and the library’s website.
- We can see the number of weekly downloads, indicating the library’s popularity.
- The list of maintainers includes Mike Bostock and Philippe Rivière, both well-known experts in data visualization.
These details (along with the GitHub repository) help us decide whether a library is trustworthy. Always be cautious before running unknown code! 🦠
Installing a library
Installing a library from npm is very easy. When you installed Node.js, you also installed npm along the way. To install d3, you only need to run one command in your terminal. First, don’t forget to stop watching your code (Control + C).
npm install d3Once the command finishes, you’ll see a few new things in your project. In the example below, the terminal reports that 38 packages were installed: D3 is made up of other modules, which npm installs too.
In the Explorer panel at the top left, you’ll see a new node_modules folder. This is where npm puts D3’s code and its dependencies on your computer for this project.
You’ll also see package.json, which lists your project’s dependencies. Open it, and you’ll find d3 there. The package-lock.json file records extra information about the packages and exact versions installed. You don’t need to worry about it for now.

You don’t need to edit node_modules or package-lock.json, and very often you won’t need to touch package.json either. You can leave these to Node.js and npm to manage. It’s just useful to know what they contain.
Using a library
Let’s reuse the Montreal temperature CSV file from the Fetching data lesson.
If you fetch this data, you’ll notice it’s just text. CSV stands for Comma-Separated Values. You can’t work with it directly. It would be much better to convert it into an array of objects.
Update main.ts with the code below, then run: node --watch main.ts
// @ts-ignore
const response = await fetch(
"https://raw.githubusercontent.com/nshiab/data-fetch-lesson/refs/heads/main/temp.csv",
);
// @ts-ignore
const data = await response.text();
console.log(data);
You might be wondering what // @ts-ignore means. It tells VS Code to ignore type errors on the next line. I added it because calling fetch with await this way requires some extra setup for TypeScript. I don’t want to bother you with that yet: for now, I just want you to focus on fetch.
It’s our lucky day: the d3 function csvParse converts CSV data into an array of objects!
To import a function from the D3 library, we use import { csvParse } from "d3". The function name goes between curly brackets, and "d3" is the name of the package we installed. Let’s update our code to use it.
import { csvParse } from "d3";
const response = await fetch(
"https://raw.githubusercontent.com/nshiab/data-fetch-lesson/refs/heads/main/temp.csv",
);
const data = await response.text();
const arrayOfObjects = csvParse(data);
console.log(arrayOfObjects);
As shown in the screenshot above, our code runs, but we have a TypeScript error. Hover over "d3" on line 1 to see the details: D3’s type definitions are missing. The pop-up also tells us how to install them. Stop watching your code (Control + C), then run this command in your terminal:
npm install @types/d3Now the error is gone, and TypeScript knows the types of D3’s functions. Run node --watch main.ts again. As you can see in the terminal below, our CSV data has been parsed into an array of objects, which will be much easier to work with in TypeScript!

In the Functions lesson, we saw that we can import functions with the syntax import myFunction from "./myFunction.ts". Here, it’s a bit different because d3 exports more than one function, and we pick just the one(s) we want. To do this, we use curly brackets with this syntax: import { myFunctionA, myFunctionB } from "./allMyFunctions.ts".
Congrats! You just installed d3 from NPM and used one of its functions! 💃🕺
Another library: csv-parse
There’s often more than one library that can solve the same problem. Let’s try csv-parse, a library dedicated to parsing CSV files. Its documentation describes the available APIs, options, examples, and a link to its source code.
Stop the running script with CTRL + C, then install it:
npm install csv-parseIf you check your package.json, you’ll now see csv-parse under dependencies as well.

Replace the code in main.ts with this, then run node --watch main.ts again:
import { parse } from "csv-parse/sync";
const response = await fetch(
"https://raw.githubusercontent.com/nshiab/data-fetch-lesson/refs/heads/main/temp.csv",
);
const data = await response.text();
const arrayOfObjects = parse(data, { columns: true });
console.log(arrayOfObjects);The columns: true option tells csv-parse to use the first row as the property names for each object. By the way, there’s no type error this time: unlike D3, csv-parse includes its TypeScript types directly in the library, so we don’t need to install them separately.
If you check your terminal, you’ll see that this new library has also converted our CSV file into an array of objects.

And voilà! You’ve installed and used a second library! 💃🕺
Conclusion
You’re probably wondering how to discover new libraries…
Well, when faced with a problem, a Google search for tutorials and blog posts can go a long way. Asking your favorite AI chatbot can help too.
And, of course, there are also wonderful courses (like this one 😏) that will introduce you to the must-have libraries!