Modules and several files
Split a program into files with require / module.exports or import / export; no npm packages.
Runs in Zerethon IDE · Node.js 22
Official JavaScript documentation ↗
main.js
const { area } = require('./geometry');
const path = require('path');
console.log(area(3, 4));
console.log(path.extname('report.json'));
geometry.js
function area(width, height) {
return width * height;
}
module.exports = { area };
Output
12 .json
main.js (the entry) and geometry.js. Local files start with ./.
- A file that uses
require()runs as CommonJS. A file that usesimport/exportruns as an ES module — Node.js 22 detects it;.mjsfiles always are. Do not mix both in one file. - With
import, write the extension:import { area } from './geometry.js'. - Node's built-in modules work (
fs,path,readline,crypto,util, …). - npm packages are not available: runs have no network and nothing is installed.