Paths & OS
The path module builds and parses file paths in a way that works across operating systems, while the os module reports information about the computer Node is running on, such as platform, CPUs, and memory.
Learn Paths & OS in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll assemble portable file paths that work on Mac, Linux, and Windows alike, take any path apart into its name, folder, and extension, and read live facts about the host machine — the everyday building blocks of real Node tools and servers.
What You'll Learn in This Lesson
1️⃣ Building Paths the Portable Way
The number-one reason the path module exists is that operating systems disagree on how to write a path. Mac and Linux use forward slashes ( /home/sam ); Windows uses backslashes ( C:\Users\sam ). If you build paths by hand with '/' , your code works on your laptop and quietly breaks on someone else's machine.
Read the worked example below — every line is explained — then run it and compare.
2️⃣ Taking a Path Apart
Just as often, you have a full path and need a piece of it — the file name for a log message, the extension to decide how to handle a file, or the folder to look inside. The path module has a function for each:
3️⃣ Reading the Machine with the os Module
The os module answers questions about the computer your program is running on. It's how tools adapt to their environment — picking a temp folder, sizing a worker pool to the CPU count, or printing a diagnostic banner.
Your turn. The program below works once you fill in the blanks marked ___ . Follow the 👉 hints, then run it and compare with the expected output.
No blanks this time — just a brief and an outline to keep you on track. Write it yourself, run it, and check your output against the example in the comments. This combines everything from the lesson: building a path, parsing it, and reading the machine.
📋 Quick Reference — path & os
Practice quiz
Which built-in module builds and parses file paths portably across operating systems?
- fs
- os
- path
- url
Answer: path. The path module joins, resolves, and parses paths using the right separator for the current OS.
Why prefer path.join('a', 'b') over writing 'a' + '/' + 'b' by hand?
- It uses the correct separator for the current OS
- It runs faster
- It encrypts the path
- It uploads the file
Answer: It uses the correct separator for the current OS. path.join inserts the right separator (/ or \) for the OS and tidies up '..' and extra slashes.
What does path.join('users', 'sam', '..', 'alex', 'todo.txt') produce?
- users/sam/alex/todo.txt
- users/alex/todo.txt
- /users/alex/todo.txt
- users/sam/../alex/todo.txt
Answer: users/alex/todo.txt. join normalizes the '..' segment, dropping 'sam' to give users/alex/todo.txt.
How does path.resolve() differ from path.join()?
- It always returns a relative path
- It removes the file extension
- It always returns an absolute path
- It only works on Windows
Answer: It always returns an absolute path. path.resolve() always produces an absolute path, falling back to the current working directory.
What does path.basename('/home/app/reports/quarter-3.csv', '.csv') return?
- quarter-3.csv
- /home/app/reports
- quarter-3
- .csv
Answer: quarter-3. basename returns the final segment, and the second argument strips the given extension, leaving quarter-3.
What does path.extname('/a/b/c.json') return?
- .json
- json
- c.json
- c
Answer: .json. extname returns the extension including the leading dot, so '.json'.
Which variable holds the folder that the currently running file lives in?
- process.cwd
- __dirname
- path.root
- os.homedir
Answer: __dirname. __dirname is the directory of the current file; joining onto it points at files beside your script.
In what units does os.totalmem() report memory?
- Gigabytes
- Megabytes
- Kilobytes
- Bytes
Answer: Bytes. os.totalmem() and os.freemem() return bytes; divide by 1024**3 for gigabytes.
How do you get the number of CPU cores with the os module?
- os.platform()
- os.cpus().length
- os.cores()
- os.hostname()
Answer: os.cpus().length. os.cpus() returns an array with one entry per core, so its length is the core count.
Do you need to install path or os from npm before using them?
- Yes, run npm install path os
- No, both are built into Node core
- Only os needs installing
- Only on Windows
Answer: No, both are built into Node core. Both path and os are core modules; just require them, no installation needed.