Variable:
Indent:
JSON Input 0 B
Ready
0 chars
JavaScript Output 0 B
JavaScript module will appear here...
0 chars
Features

Why use this converter

ES modules

Output as export const data = {...}; for modern bundlers (Vite, esbuild, webpack 5). Import with import {{ data }} from './data.js'.

CommonJS

For Node.js, output as var data = {...}; or module.exports = {...};. Compatible with require() in any Node version.

Pretty-printed

The embedded JSON is pretty-printed with your chosen indentation, making the output file readable and diff-friendly.

How it works

Conversion explained

1. Parse JSON

Input is parsed into a JavaScript value. Invalid JSON produces a clear error message.

2. Serialize

The value is serialized with JSON.stringify(value, null, indent). The output is valid JavaScript (JSON is a strict subset of JS).

3. Wrap in export

Based on your options, the serialized value is wrapped in export const name = ...; (ES module) or var name = ...; (legacy).

FAQ

JSON to JavaScript Module Converter — frequently asked questions

What's the difference between ES modules and CommonJS?

ES modules use import/export syntax and work in modern browsers and bundlers. CommonJS uses require/module.exports and works in Node.js without configuration.

Can I convert JavaScript back to JSON?

Yes — use the JSON Formatter tool. Paste the JS, remove the export/var prefix, and the formatter will parse the JSON value.

Is the output minified?

No — by default, the output is pretty-printed with 2 spaces. To minify, choose "Minified" in the indent dropdown.

Does this work for tree-shaking?

Yes — ES module exports are statically analyzable, so bundlers like webpack and Rollup can tree-shake unused properties.