json-to-es-module

2.0.2 • Public • Published

json-to-es-module

npm version Build Status codecov

Convert JSON to an ECMAScript module

{
  "name": "Sam"
}

export default {
  name: 'Sam'
};

Installation

Use npm.

npm install json-to-es-module

API

const jsonToEsModule = require('json-to-es-module');

jsonToEsModule(str [, option])

str: string (JSON string)
option: Object
Return: string

jsonToEsModule(`{
  "foo": 1,
  "bar": [
    true,
    null
  ]
}`);
//=> 'export default {\n\tfoo: 1,\n\tbar: [\n\t\ttrue,\n\t\tnull\n\t]\n};\n'

option.filename

Type: string

Filename displayed in the error message.

try {
  jsonToEsModule('"');
} catch (err) {
  err.message; //=> Unexpected end of JSON input while parsing near '"'
}

try {
  jsonToEsModule('"', {filename: 'source.json'});
} catch (err) {
  err.message; //=> Unexpected end of JSON input while parsing near '"' in source.json
}

License

ISC License © 2018 - 2019 Shinnosuke Watanabe

Package Sidebar

Install

npm i json-to-es-module

Weekly Downloads

32

Version

2.0.2

License

ISC

Unpacked Size

4.58 kB

Total Files

4

Last publish

Collaborators

  • shinnn