Essentials

Path, url, querystring and util

In Node.js, handling file paths and web URLs requires specific modules to ensure cross-platform compatibility and correct data parsing.

1. The Path Module

The path module provides utilities for working with file and directory paths. This is essential because Windows uses backslashes (\) while POSIX (Linux/macOS) uses forward slashes (/).


Core Methods


  • path.join(...paths): Joins segments into a single path using the correct platform-specific separator.

  • path.resolve(...paths): Resolves a sequence of paths into an absolute path.

  • path.extname(path): Returns the extension (e.g., .js, .html).

  • path.parse(path): Returns an object with details like root, dir, base, and name.


const path = require('path');

const fullPath = path.join('users', 'admin', 'config.json');
console.log(fullPath); 
// Windows: users\admin\config.json
// POSIX: users/admin/config.json

console.log(path.extname('index.html')); // .html
console.log(path.parse('/home/user/app.js'));
/* 
{ root: '/', dir: '/home/user', base: 'app.js', ext: '.js', name: 'app' } 
*/



2. The URL Module


The url module is used for URL resolution and parsing. Modern Node.js uses the WHATWG URL Standard, which is the same API used in web browsers.

The URL Object

When you create a new URL instance, Node.js breaks it down into its components: protocol, hostname, pathname, and searchParams.


const { URL } = require('url');

const myUrl = new URL('https://example.com:8080/p/a/t/h?query=string&id=123#hash');

console.log(myUrl.host);     // example.com:8080
console.log(myUrl.pathname); // /p/a/t/h
console.log(myUrl.search);   // ?query=string&id=123


3. Query String Handling


The Query String is the part of the URL that starts after the ?. In modern Node.js, the preferred way to handle this is via the URLSearchParams API.

URLSearchParams API

This provides an easy way to get, set, and iterate over query parameters.

const myUrl = new URL('https://site.com/search?user=dev&lang=en');

const params = myUrl.searchParams;

console.log(params.get('user')); // dev
console.log(params.has('lang')); // true

// Adding or modifying parameters
params.append('page', '1');
console.log(myUrl.toString()); // https://site.com/search?user=dev&lang=en&page=1


The Legacy querystring Module

Before the URL API, Node.js used a module called querystring. While it still exists, it is considered legacy.

  • querystring.parse(): Converts a string like foo=bar&baz=qux into an object.

  • querystring.stringify(): Converts an object back into a string.


4. Path vs. URL: A Common Pitfall


One major difference in Node.js (especially when using ES Modules) is that file paths and URLs are different types.

  • __dirname (CommonJS) is a plain string path.

  • import.meta.url (ESM) is a file:// URL string.

To convert between them safely, use the built-in conversion tools:

const { fileURLToPath, pathToFileURL } = require('url');

const filePath = fileURLToPath('file:///C:/path/to/file.js'); 
// Converts URL to a valid system path string


.