The Node.js ecosystem

You've built a working ingredients app. Congratulations - go and put the kettle on.

This section is about what makes Node.js more than "JavaScript on a server." The runtime itself is deliberately small. Unlike RPG or Java, Node does not try to ship every tool you will ever need. The deal is closer to the old Unix idea:

  • write a module that does one thing, and does it well
  • write modules that fit together
  • prefer streams and events over one giant program that does everything

The payoff is the ecosystem: hundreds of thousands of packages on npm, the Node Package Manager, plus the ones you write yourself. We've already been using npm in Sections 4 and 5 (`express`, `odbc`, `itoolkit`, `dotenv`, `pm2`). In this lesson we'll slow down and look at how it actually works. The next three lessons then take three packages for a proper spin: a project generator, Excel spreadsheets, and email.

These ecosystem lessons take their cue from Scott Klement's session *The World of Node.js on IBM i* (with David Russo). I've rewritten the material for this course, pointed the examples at our `FOODLIB.INGREDIENTS` table, and updated them for Node.js 24. Scott's slides and sample code are at [scottklement.com/presentations].

Modules are service programs in a funny hat

A module is a library you can pull into your program. The built-in ones (http, fs, path) come with Node.js. Everything else you either install with npm or write yourself.

You bring a module in with require, and you call whatever it exports:

const http = require('http'); // built in - no install needed
const odbc = require('odbc'); // from npm - you ran npm install odbc
const db = require('./db'); // your own file, db.js, in the same folder

That last one is exactly what we did in Lesson 5.2. ./db means "the db.js file next to me," and module.exports = { query, close } on the other side is how that file publishes its API. If you've ever written a service program and a binding directory, you already understand this. The file is the module.

A quick word on import. Node.js 24 also understands ESM import / export syntax. This course sticks to require and module.exports (CommonJS) because that's what our earlier lessons use, and it works perfectly. If a package's README shows only import, you can usually still require it - or add "type": "module" to package.json and switch a project over. Don't mix the two styles in the same file.

The npm commands you'll actually use

npm talks to the registry at [npmjs.com] and to the node_modules folder in your project. Run these from a PASE shell - the SSH session or VS Code terminal we set up in Section 3. (A nice Unix-style terminal is happier with npm's coloured output than a 5250 QSH session, though both will work.)

npm cheat sheet

By default, packages are installed locally, in a node_modules folder next to your package.json. That is what you want for application dependencies. The project's package.json becomes the shopping list, and package-lock.json pins the exact versions so the next machine (or the next Tuesday) gets the same tree.

Local, global, and npx

Local (npm install exceljs) - lives in this project. This is the default, and the right choice for libraries your code require's.

Global (npm install -g or the older --global) - lives once on the system and puts a command on your PATH. Handy for tools you run from the shell, such as pm2. On IBM i, a global install lands under the Node.js you selected with nodever (for us, nodejs24), so a nodever switch can change which global tools you see. Check with your fellow developers before you go installing -g on a shared box.

npx - runs a package's command without a global install. Node.js 24 ships it with npm. Prefer this for one-off tools (we'll use it in the next lesson):

npx express-generator --help

Pinning a version works the same locally or globally:

npm install exceljs@4.4.0
npm install -g pm2@5.4.0

A tiny module of your own

Just to close the loop, here's a module that isn't a 200-line database wrapper. Create a folder, then two files:

bash

mkdir -p ~/modules-demo && cd ~/modules-demo

javascript

// greet.js
function greet(name) {
return `Hello, ${name}, from Node.js on IBM i.`;
}

module.exports = { greet };
// app.js
const { greet } = require('./greet');

console.log(greet('Nick'));

then back bash lets run it!

bash

node app.js
# Hello, Nick, from Node.js on IBM i.

That's the whole trick. Everything we install from npm in the next lessons is the same shape: npm install, require, call a function.

A word about debugging

VS Code or IBM BOB (with the Code for IBM i extension, over SSH) is the comfortable option. Open the project folder, set a breakpoint in the gutter, and start the debugger on node with your script. VS Code's built-in debugger is very good, and it's free.

From the shell, Node.js has an inspector built in:

node --inspect-brk ingredients-report.js

That pauses on the first line and prints a ws:// URL. Open chrome://inspect in a Chrome browser (or use the VS Code "Attach" debugger) and you can step through the code, even when it's running on the IBM i. When you're finished, `Ctrl+C` stops the process the same way it did for our Hello World server in the earlier lesson.

What's next?

The ecosystem is huge. In the next lesson we'll use a generator to scaffold an Express app in a few seconds. After that, we'll pull a report out of Db2 for i into a real Excel workbook, and email it - the sort of nightly job every IBM i shop already understands, just written in Node.js.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>