typestar

Paths in JavaScript

join, resolve, and taking a filename apart.

import { basename, dirname, extname, join, parse, relative, resolve }
  from "node:path";

const path = join("src", "typestar", "..", "seeds", "go", "main.go");
console.log(path);
console.log(basename(path), extname(path), dirname(path));
console.log(parse(path).name, parse(path).ext);

console.log(resolve("/var", "log", "../data").endsWith("/var/data"));
console.log(relative("/a/b/c", "/a/d"));
console.log(typeof import.meta.dirname);

How it works

  1. join normalizes; resolve makes it absolute.
  2. parse splits into dir, name and ext.
  3. import.meta.dirname replaced the __dirname dance in ESM.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
437
Tokens
126
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 76 seconds.

Type this snippet

Step 2 of 2 in Modules & paths, step 2 of 18 in Node.js.

← Previous Next →