Why convert MJS to JSON?
Sometimes a config or fixture lives in an .mjs module and you need it as a static .json file that other tools can read without running JavaScript, so you execute the module and serialize its exported value.
How to convert MJS to JSON
Node.js one-liner FREE
Import the module and print JSON: node --input-type=module -e "import('./file.mjs').then(m=>console.log(JSON.stringify(m.default,null,2)))" > file.json.
Small Node script FREE
Write a short .mjs that does import data from './file.mjs' then fs.writeFileSync('file.json', JSON.stringify(data, null, 2)) and run it with node.
Visual Studio Code FREE
If the .mjs already exports a plain object literal, copy that object into a new .json file and remove the export keyword, quoting all keys.
About these formats
An .mjs file is JavaScript source code that Node.js treats as an ECMAScript module, meaning it uses import and export instead of CommonJS require. The extension exists so Node can tell ES…
Open .MJS details →A JSON file is a plain-text data file in JavaScript Object Notation format, used by web APIs, app configurations, and data exports. Any text editor opens it - Notepad on Windows, TextEdit…
Open .JSON details →Quality & what to watch
- Only serializable data survives: functions, classes,
undefined,BigInt, symbols and comments are dropped or throw duringJSON.stringify. - Running the module executes its code, so any side effects (network calls, file writes) happen during conversion.
- If the module builds its export dynamically at runtime, the JSON is only a snapshot of one particular run.
Frequently asked questions
Why can't I convert `.mjs` straight to `.json`?
.mjs is executable code that can contain logic, imports and functions, while JSON holds only static values. There is nothing to convert unless you run the code first.How do I get JSON out of an `.mjs`?
JSON.stringify. Write the result to a .json file.What gets lost?
undefined, BigInt, symbols and comments. Only plain data remains.