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
joinnormalizes;resolvemakes it absolute.parsesplits into dir, name and ext.import.meta.dirnamereplaced the __dirname dance in ESM.
Keywords and builtins used here
constfromimport
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.
Step 2 of 2 in Modules & paths, step 2 of 18 in Node.js.