Use new URL('./settings.json', import.meta.url) when the JSON file lives beside an ES module. Use readFile() with a path resolved from the current working directory when the user supplies the file to a command-line program. Both approaches can read and parse the same file. The difference is where Node starts looking when you launch the program from somewhere else.
Create a tiny project you can move around
Set up a demo folder inside an empty folder. Save this as demo/settings.json. Keep the double quotes and avoid a trailing comma. Start with the file exactly as shown so that any first failure is easier to diagnose.
{
"mode": "demo",
"refreshSeconds": 30
}
We will add three .mjs files next to it. The extension tells Node to treat them as ES modules even without a package configuration. The examples use modern Node.js, and the static import example uses current JSON import attributes. Check your version with node --version if the static import syntax raises an error. Node.js ES modules
Read the JSON next to the module
Save this as demo/beside.mjs. This is a fit for a bundled settings file, such as a default configuration that is checked into the same project as the module.
import { readFile } from 'node:fs/promises';
const url = new URL('./settings.json', import.meta.url);
const text = await readFile(url, 'utf8');
const settings = JSON.parse(text);
console.log(settings.mode);
The file URL starts with the URL of the module. The relative part points to the neighbouring JSON file. Node’s filesystem API accepts the resulting file URL directly. The ‘utf8’ argument gives you a string, and JSON.parse() turns the string into a JavaScript value. Node.js filesystem documentation
From the parent of demo, run both commands below. The first line launches the script from the parent. The last line launches it again from inside the project. Each launch should print demo because the file sits beside the module in both runs.
node demo/beside.mjs
cd demo
node beside.mjs
The entry path you give to Node is a separate thing. If you spell demo/beside.mjs incorrectly, Node cannot launch the script. If Node runs the script but reports that opening the JSON file failed, the entry script was located and you can inspect the JSON path. See how to diagnose a missing local module when the error originates at the import or entry step.
Read a path relative to the user’s starting folder

A command-line tool often takes a file argument. People expect settings.json to refer to the file in the folder where they ran the tool, just as it would in a shell command. Save this version as demo/from-cwd.mjs.
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
const file = process.argv[2] ?? 'settings.json';
const filePath = resolve(file);
const text = await readFile(filePath, 'utf8');
const settings = JSON.parse(text);
console.log(settings.mode);
From the parent, the explicit argument needs the demo/ prefix. From insidedemo, the default filename works because the current directory now contains the file.
node demo/from-cwd.mjs demo/settings.json
cd demo
node from-cwd.mjs
To check the difference on purpose, go back to the parent folder and run node demo/from-cwd.mjs without the argument. In this sample setup, it should report ENOENT because the parent has no settings file. If it succeeds, there may already be a settings.json next to the demo folder. Print filePath to check which one was read.
Use this approach for a trusted local command-line file argument. If the code later moves behind a web endpoint, decide which files the application is allowed to read before accepting paths from remote callers. The example simply honours the local user’s choice.
Use a static import for data shipped with the module
In a modern Node.js ES module, a static JSON import is concise when the file is known ahead of time. Save this as demo/static.mjs.
import settings from './settings.json' with { type: 'json' };
console.log(settings.mode);
The import is resolved relative to the importing file. The JSON module provides a default export and requires the JSON type attribute in current Node.js. It is a good option for data packaged with the app.
If you need to pick a different file per invocation or explicitly re-read an edited file during one long-running process, the filesystem example makes the timing and path easier to see. Node.js JSON modules
Read the error in two steps
If the file cannot be opened, inspect the path and check whether it should start at the module or at the working directory. Printing url.href in the first example or filePath in the second tells you where Node looked.

Once the file opens, a syntax error from JSON.parse() points to the contents. Look for missing double quotes, an incomplete value, or a trailing comma.
A successful parse also leaves you with the question of what shape you expected. A file can contain a valid array, number, or null when your code needs an object with a mode property. Check the shape separately before you use the value.
Make the resolution visible in a third directory
Create a new folder beside your demo and give it a different settings.json with the same properties but a different mode value, such as caller. Enter the new folder before launching the scripts. If you launch the sibling-module example from there by giving Node the correct path to the script, it should still print demo.
If you launch the working-directory example without a path argument from the same folder, it should now print caller. Both calls succeed, but they select different files. That makes this a useful check when a real application runs without errors while quietly loading the wrong configuration.
On Windows, quote any path with spaces when passing it through a shell. The quotes are part of how you give the shell one argument. Once Node receives the argument, resolve can work with the string it receives.
File URLs can also represent spaces in a module path, which is a reason to keep the URL as a URL instead of manually taking its pathname and treating it as an ordinary Windows filename. Node provides fileURLToPath when you actually need an operating-system path string. Node.js URL utilities
Give loading and validation different error messages
In a CLI, a good error identifies the file and the step. If reading the file fails, show the path or URL the program attempted to open.
An ENOENT means the path or a component of it was unavailable, so check spelling, working directory, and whether the file was copied into the expected location. An access error points toward a different check. Avoid replacing the original error code with a message that makes every problem look like invalid JSON.
After a successful read, decide how you will report parse errors. A malformed comma or smart quote may be hard to see in a long file. Name the input path and let the original syntax error help the caller find the problem.
For a configuration file that may contain secrets, keep file contents out of the message. The path and the error type are usually enough to start.
Once the value parses, check the properties that your code relies on. Ask whether the top-level value is an object and whether mode has the type and allowed values you expect. If you accept refreshSeconds, decide whether zero has a meaning in your program and whether you need a whole number.
Apply defaults only where the contract allows a missing value. Validate the shape of parsed JSON in JavaScript shows what to do at that next step.
Pick the loading point for a real project
A file sent with your script
Use the module-relative approach for application defaults, fixture files, or other resources that are distributed beside the script. Check the result of your packaging step too.
A URL can point to the intended place and still fail to open if the JSON was skipped when the JavaScript files were copied. Try running the packaged copy from outside its own directory before you distribute it. You are checking both the layout and the path contract.
A file selected by the caller
Use the command-line approach for a data file the caller chooses. Document whether the default is a filename in the working directory and show one command with an explicit path.
This matters in scheduled tasks and editor run configurations, which may set a different starting directory from the one you see in an interactive terminal. A filename that works for you locally may still be perfectly valid when launched from a different folder with an explicit argument.
The tool needs to notice edits
Read again at the point you want to see a fresh copy. If you keep the parsed result in a variable after program startup, later disk edits will not change that variable. Think about what should happen if someone saves a partial file while the program reads it.
You can preserve the last valid configuration and report the new problem if that matches the application’s needs. A static import makes sense where the file should follow module loading and the same value can be reused.
Write down the path contract next to the example command in your project documentation. The most valuable sentence is often as simple as “Relative settings paths are resolved from the folder where you run the command.” That gives people a concrete expectation before they encounter an error.
When writing a reproduction for a teammate, give both the command and the directory where you ran it. Showing only the code leaves out the input that can explain the entire failure.
If your command works on a second machine, compare the directory and the actual file that each run printed before changing the JSON or reinstalling dependencies.
When you finish, run the module-relative and working-directory examples from both locations one more time. Knowing which directory is supposed to control the path gives you a quick way to predict which runs should succeed.
