Registry / devops / json-schema-loader

json-schema-loader

JSON →
library1.0.0jsnpmunverified

Webpack loader that resolves internal and external JSON Schema references ($ref) using json-schema-ref-parser. Version 1.0.0 is the first stable release. Supports both JSON and YAML schemas. Part of the Cloudflare doca suite. Provides options like useSource to enable chaining with other loaders. Useful for any webpack project that needs to resolve JSON Schema references at build time.

npm install json-schema-loader
INSTALL
IMPORT
SIG · JSON-SCHEMA-LOADER
J
json-schema-loader
devopsjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

resolvedSchema
import resolvedSchema from 'json-schema-loader!./schema.json';
const resolvedSchema = require('json-schema-loader!./schema.json');
Loader syntax is used in webpack. The loader returns the resolved schema object as a default export.
webpack rule
module.exports = { module: { rules: [ { test: /\.json$/, exclude: /node_modules/, loader: 'json-schema-loader' } ] } };
module.exports = { module: { loaders: [ { test: /\.json$/, exclude: /node_modules/, loader: 'json-schema-loader' } ] } };
Webpack 4+ uses 'rules' instead of 'loaders'. The loader is compatible with webpack 4 and 5.
options
import schema from 'json-schema-loader?useSource=true!./schema.json';
const schema = require('json-schema-loader!./schema.json?useSource=true');
Query parameters are passed to the loader's options. 'useSource' is a boolean option.

Shows how to configure webpack to use json-schema-loader for all JSON files except node_modules, and then import a resolved schema in a module.

// webpack.config.js module.exports = { module: { rules: [ { test: /\.json$/, exclude: /node_modules/, loader: 'json-schema-loader', options: { useSource: false } } ] } }; // app.js import schema from './schema.json'; console.log(schema);
Debug
Known issues
gotchaLoader does not work with webpack's built-in JSON parser if you chain loaders without setting useSource=true.
fix
Set options.useSource=true to pass the raw source to json-schema-ref-parser instead of the file path.
affects: >=1.0.0
deprecatedWebpack loaders syntax with require or import statements using ! is deprecated in favor of inline loader syntax.
fix
Prefer configuring the loader in webpack.config.js rather than using inline loader syntax.
affects: >=1.0.0
gotchaThe loader expects JSON or YAML files. Using it on non-schema files will cause runtime errors or unexpected behavior.
fix
Ensure the test pattern only matches relevant schema files, or use exclude patterns to avoid non-schema JSON files.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Module parse failed: Unexpected token in JSON at position 0
The loader is chained with another loader that outputs JavaScript, but useSource is not set to true.
fix
Set options.useSource=true so the loader processes the raw source before other loaders.
Error: Cannot find module 'json-schema-loader'
json-schema-loader is not installed or not in node_modules.
fix
Run npm install json-schema-loader --save-dev.
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
Using inline loader syntax with require might cause webpack to warn about dynamic requires.
fix
Configure the loader in webpack.config.js instead of using inline syntax.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
json-schema-ref-parserrequiredCore dependency used to resolve $ref references in JSON schemas.
Agent activity
10 hits · last 30 days
node
8
Resources
json-schema-loader — npm install json-schema-loader · libregistry