Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

json5-loader

webpack-contrib90.2kMIT4.0.1

json5 loader module for webpack

webpack

readme

npm node deps tests cover chat size

json5-loader

A webpack loader for parsing json5 files into JavaScript objects.

Getting Started

To begin, you'll need to install json5-loader:

$ npm install json5-loader --save-dev

You can use the loader either:

  • by configuring the json5-loader in the module.rules object of the webpack configuration, or
  • by directly using the json5-loader! prefix to the require statement.

Suppose we have the following json5 file:

file.json5

{
  env: 'production',
  passwordStrength: 'strong',
}

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.json5$/i,
        loader: 'json5-loader',
        type: 'javascript/auto',
      },
    ],
  },
};

Options

Name Type Default Description
esModule {Boolean} true Uses ES modules syntax

esModule

Type: Boolean Default: true

There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.

You can enable a ES module syntax using:

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.json5$/i,
        loader: 'json5-loader',
        options: {
          esModule: false,
        },
        type: 'javascript/auto',
      },
    ],
  },
};

Examples

Usage with require statement loader prefix

file.json5

{
  env: 'production',
  passwordStrength: 'strong',
}

index.js

import appConfig from 'json5-loader!./file.json5';

console.log(appConfig.env); // 'production'

Don't forget to polyfill require if you want to use it in Node.js. See the webpack documentation.

Contributing

Please take a moment to read our contributing guidelines if you haven't yet done so.

CONTRIBUTING

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.0.1 (2020-10-09)

Chore

  • update schema-utils

4.0.0 (2020-04-15)

Features

  • implement the esModule option

⚠ BREAKING CHANGES

  • minimum required nodejs version is 10.13.0
  • use ES module syntax by default

3.0.0 (2019-06-05)

chore

BREAKING CHANGES

  • deps: minimum required nodejs version is 8.9.0

2.0.0 (2019-03-22)

Features

  • update json5 to 2.1.0 version
  • use JSON5.stringify for output

Bug Fixes

  • avoid swallowing JSON5 error output (#34) (d050827)
  • support for Infinity && NaN (#26) (5a8ca43)
  • fix output \u2028\u2029 characters

BREAKING CHANGES

  • drop support for webpack < 4
  • drop support for node < 6.9
  • update json5 to 2.1.0 version
  • use JSON5.stringify for output

2.0.0-beta.0 (2017-05-01)

Code Refactoring

BREAKING CHANGES

  • Enforces a minimum NodeJS version of 4.3 via engines