stringify
raw JSON → 5.2.0 verified Sat Apr 25 auth: no javascript maintenance
Browserify transform to require() text files (HTML, templates, etc.) as strings in client-side JavaScript. Version 5.2.0 is the latest release; the package is no longer actively maintained by the author but accepts PRs. It supports Node >=4.0.0 (tested up to 8.1.3). Key differentiators: simple setup, optional minification via html-minifier, Node.js require hook. Alternatives like brfs or rawify are more actively maintained.
Common errors
error Error: Cannot find module 'html-minifier' ↓
cause Missing dependency when minify option is enabled.
fix
Run 'npm install html-minifier --save' or disable minify option.
error TypeError: Cannot read property 'includeExtensions' of undefined ↓
cause appliesTo option not provided correctly when calling .transform().
fix
Provide an object with appliesTo.includeExtensions array, e.g., { appliesTo: { includeExtensions: ['.html'] } }.
error SyntaxError: Unexpected token ILLEGAL in JSON at position 0 ↓
cause Using require() on a file without applying the transform first.
fix
Ensure stringify transform is applied in Browserify config before bundling.
Warnings
deprecated Package no longer actively maintained; open issues may not be addressed. ↓
fix Consider migrating to brfs, rawify, or other actively maintained transforms.
gotcha The transform MUST be applied via .transform() before .add() — order matters. ↓
fix Always call .transform() first, then .add().
gotcha Node.js usage via stringify.registerWithRequire() might conflict with Browserify usage if both are used in same process. ↓
fix Use separate processes or modules for Node.js and Browserify builds.
gotcha Requiring configuration from package.json may lead to unexpected behavior if key is a file path relative to cwd. ↓
fix Ensure the path in the 'stringify' key is correct relative to process.cwd().
Install
npm install stringify yarn add stringify pnpm add stringify Imports
- stringify wrong
import stringify from 'stringify';correctconst stringify = require('stringify'); - stringify.registerWithRequire wrong
require('stringify').registerWithRequire({...}); (works but less common)correctstringify.registerWithRequire({...}); - browserify transform wrong
browserify().transform('stringify', ...)correctbrowserify().transform(stringify, { appliesTo: { includeExtensions: ['.html'] } })
Quickstart
const browserify = require('browserify');
const stringify = require('stringify');
const fs = require('fs');
const b = browserify();
b.transform(stringify, {
appliesTo: { includeExtensions: ['.html'] }
});
b.add('./entry.js');
b.bundle().pipe(fs.createWriteStream('bundle.js'));
// entry.js:
// const template = require('./template.html');
// console.log(template);