KKitForma.

Language

EnglishEnglishTürkçeTurkishGuide unavailable in this language · open toolsDeutschGermanGuide unavailable in this language · open toolsEspañolSpanishGuide unavailable in this language · open toolsFrançaisFrenchGuide unavailable in this language · open toolsPortuguêsPortugueseGuide unavailable in this language · open toolsItalianoItalianGuide unavailable in this language · open toolsNederlandsDutchGuide unavailable in this language · open toolsPolskiPolishGuide unavailable in this language · open toolsРусскийRussianGuide unavailable in this language · open toolsУкраїнськаUkrainianGuide unavailable in this language · open toolsSvenskaSwedishGuide unavailable in this language · open toolsNorskNorwegianGuide unavailable in this language · open toolsDanskDanishGuide unavailable in this language · open toolsSuomiFinnishGuide unavailable in this language · open toolsČeštinaCzechGuide unavailable in this language · open toolsRomânăRomanianGuide unavailable in this language · open toolsΕλληνικάGreekGuide unavailable in this language · open toolsالعربيةArabicGuide unavailable in this language · open toolsעבריתHebrewGuide unavailable in this language · open toolsفارسیPersianGuide unavailable in this language · open toolsاردوUrduGuide unavailable in this language · open toolsहिन्दीHindiGuide unavailable in this language · open toolsবাংলাBengaliGuide unavailable in this language · open toolsதமிழ்TamilGuide unavailable in this language · open toolsతెలుగుTeluguGuide unavailable in this language · open toolsमराठीMarathiGuide unavailable in this language · open toolsગુજરાતીGujaratiGuide unavailable in this language · open tools简体中文Chinese SimplifiedGuide unavailable in this language · open tools繁體中文Chinese TraditionalGuide unavailable in this language · open tools日本語JapaneseGuide unavailable in this language · open tools한국어KoreanGuide unavailable in this language · open toolsTiếng ViệtVietnameseGuide unavailable in this language · open toolsไทยThaiGuide unavailable in this language · open toolsBahasa IndonesiaIndonesianGuide unavailable in this language · open toolsBahasa MelayuMalayGuide unavailable in this language · open toolsFilipinoFilipinoGuide unavailable in this language · open toolsKiswahiliSwahiliGuide unavailable in this language · open toolsAfrikaansAfrikaansGuide unavailable in this language · open toolsMagyarHungarianGuide unavailable in this language · open toolsБългарскиBulgarianGuide unavailable in this language · open toolsHrvatskiCroatianGuide unavailable in this language · open toolsSrpskiSerbianGuide unavailable in this language · open toolsSlovenčinaSlovakGuide unavailable in this language · open toolsSlovenščinaSlovenianGuide unavailable in this language · open toolsLietuviųLithuanianGuide unavailable in this language · open toolsLatviešuLatvianGuide unavailable in this language · open toolsEestiEstonianGuide unavailable in this language · open toolsCatalàCatalanGuide unavailable in this language · open toolsEuskaraBasqueGuide unavailable in this language · open tools
← Questions & answers

KITFORMA · EDITORIAL ANSWER

How do I locate a data file beside an ES module without relying on cwd?

KitForma editorial guide. Resolve an asset against import.meta.url so a Node.js ES module can find it from any working directory.

Node.js, HTTP & APIs →

Step-by-step answer

Relative filesystem paths normally use the process working directory. An asset beside an ES module should instead be resolved against that module’s URL. Save the two files below in the same directory, including a space or non-ASCII character in the directory name for a useful check. data.json:
json
{"sku":"00125","name":"Ada"}
Runnable example:
javascript
import { readFile } from "node:fs/promises";
const asset = new URL("./data.json", import.meta.url);
const data = JSON.parse(await readFile(asset, "utf8"));
console.log(JSON.stringify(data));
Expected result:
text
{"sku":"00125","name":"Ada"}
Why it works: Name the JavaScript file reader.mjs and run node followed by its quoted absolute path from a different directory. import.meta.url follows reader.mjs; changing cwd therefore does not change the asset location. Node’s readFile accepts a file URL directly, avoiding manual decoding of Windows drive letters, spaces or percent escapes. Scope and checks: Node.js 24 ES modules. If a library requires a path string, use fileURLToPath(asset) from node:url instead of asset.pathname. The asset must be included in the deployment package. This pattern does not by itself allow filesystem access in browser JavaScript or make JSON import attributes portable across runtimes. Related tools and guides: Inspect the sidecar JSON data: https://kitforma.com/en/json-formatter/ Handle file operations without a pre-check: https://kitforma.com/en/community/questions/7aa10000-0000-4000-8000-000000008520/ Primary sources (reviewed 2026-10-08): Node.js — fileURLToPath and platform path conversion: https://nodejs.org/api/url.html#urlfileurltopathurl-options Node.js 24 — file URL paths: https://nodejs.org/docs/latest-v24.x/api/fs.html#file-url-paths

Sources and verification

Sources checked:

This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.

Discussion and community answers

Loading community…
KitForma

What would you like to do?