nearley-loader
raw JSON → 2.0.0 verified Sat Apr 25 auth: no javascript
A webpack loader that imports compiled nearley parser grammar files (`.ne` files) directly into JavaScript modules. Current stable version is 2.0.0, compatible with nearley^2.0.0. It integrates with webpack's module bundling, allowing developers to require grammar files and get compiled parser rules. The loader has a peer dependency on nearley, requiring matching major versions. It is commonly used in projects that build parsers with nearley and bundle with webpack. No release cadence is documented; the package is stable but low-maintenance.
Common errors
error Cannot find module 'nearley-loader' ↓
cause nearley-loader is not installed or is a devDependency not resolved.
fix
Run: npm install --save-dev nearley-loader
error Module not found: Error: Can't resolve 'nearley-loader' in ... ↓
cause Webpack cannot locate the loader, likely missing from node_modules.
fix
Ensure nearley-loader is in devDependencies and run npm install.
error TypeError: nearley.Grammar.fromCompiled is not a function ↓
cause Using a very old nearley version (before fromCompiled was introduced, e.g. <2.0.0).
fix
Update nearley to >=2.0.0: npm install nearley@latest
error The 'nearley-loader' loader is not being recognized in webpack.config.js ↓
cause Loader path or regex may be misconfigured; or webpack version is too old.
fix
Use the correct rule definition as shown in the quickstart.
Warnings
gotcha Requiring a grammar file with CommonJS require will import the compiled rules object as default (not a module.exports keyed object). ↓
fix Use ESM import syntax (import grammar from './grammar.ne') for clarity.
gotcha Grammar.fromCompiled must be called on the imported value; passing the raw object to Parser constructor fails. ↓
fix Always use Grammar.fromCompiled(grammar) to get a Grammar instance.
breaking Major version must match nearley's major version. nearley-loader@2.x works only with nearley@^2.0.0; nearley-loader@1.x with nearley@^1.0.0. ↓
fix Install compatible versions: npm install nearley@^2.0.0 nearley-loader@^2.0.0
gotcha create-react-app applies url-loader to unrecognized extensions; .nearley files may be incorrectly handled without override. ↓
fix In CRA, use craco or react-app-rewired to override the webpack config and exclude .nearley from url-loader.
gotcha TypeScript users may need declarations for .ne files; the package does not export types. ↓
fix Add a module declaration: declare module '*.nearley' { const rules: any; export default rules; }
Install
npm install nearley-loader yarn add nearley-loader pnpm add nearley-loader Imports
- default import of grammar wrong
const grammar = require('./grammar.ne');correctimport grammar from './grammar.ne'; - Parser and Grammar classes wrong
import { Parser, Grammar } from 'nearley-loader';correctimport { Parser, Grammar } from 'nearley'; - Grammar.fromCompiled wrong
const parser = new Parser(grammar);correctconst parser = new Parser(Grammar.fromCompiled(grammar));
Quickstart
// webpack.config.js
module.exports = {
module: {
rules: [
{ test: /\.nearley$/i, use: ['nearley-loader'] },
],
},
};
// yourModule.js
import { Parser, Grammar } from 'nearley';
import grammar from './grammar.ne';
const parser = new Parser(Grammar.fromCompiled(grammar));
parser.feed('some input');
console.log(parser.results);