Skip to content

Latest commit

 

History

History
34 lines (21 loc) · 1.15 KB

File metadata and controls

34 lines (21 loc) · 1.15 KB

🐢 Node.js

🌟 The different core modules

Path

The path module provides utilities for working with file and directory paths.

It will mostly be used for path composition (if you do them yourself your code is probably not cross-platform).

const path = require("path");

// ⛔️ BAD
const bad = __dirname + "/" + "path";

// ✅ GOOD
const good = path.join(__dirname, "path");

The paths are not built in the same way depending on the operating system that will be used (UNIX, Windows, MAC etc). If you are interested in cross-platform code writing best practices, I recommend Awesome Node.js cross platform.

The path module also has a set of methods that will allow you to retrieve the different parts that make up the path of a file or folder. Like for example the extension of a file (that all juniors have fun recovering with a RegExp or with hacks .split etc).

const path = require("path");

console.log(path.extname("boo.js")); // .js

⬅️ 🌟 The different core modules: Events | ➡️ 🌟 The different core modules: FileSystem