Registry / serialization / umd
library3.0.3jsnpmunverified

The `umd` package, currently at version 3.0.3, provides a specialized utility for wrapping JavaScript modules with Universal Module Definition (UMD) boilerplate. It is primarily designed for integration into automated build systems, enabling developers to convert their proprietary or standard modules into a format compatible with a wide array of JavaScript environments, including Asynchronous Module Definition (AMD) loaders, CommonJS environments (like Node.js), and traditional browser global script tags. The library supports two primary input formats: a 'return style' module, where the module's export is the direct result of a `return` statement, and CommonJS-style modules which require an explicit `commonJS: true` option. A key differentiator is its synchronous operation and its focus on preventing naming conflicts between multiple UMD modules on the same page. The package offers functions to generate the UMD prelude, postlude, or to wrap an entire module source string. While UMD was crucial for cross-environment compatibility, its necessity has somewhat diminished with the widespread adoption of native ECMAScript Modules (ESM) in modern environments.

npm install umd
INSTALL
IMPORT
SIG · UMD
U
umd
serializationjavascriptv3.0.3
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

umd
✓ const umd = require('umd');
✗ import umd from 'umd';
The primary `umd` function is part of the module's default export. It is predominantly used in Node.js build scripts, which typically use CommonJS `require`.
umd.prelude
✓ const umd = require('umd'); const prelude = umd.prelude('my-module-name');
Accesses the `prelude` utility function to get the opening UMD boilerplate for a module. Available via the default CommonJS export.
umd.postlude
✓ const umd = require('umd'); const postlude = umd.postlude('my-module-name');
Accesses the `postlude` utility function to get the closing UMD boilerplate for a module. Available via the default CommonJS export.

Demonstrates how to use the `umd` package programmatically to wrap both 'return style' and CommonJS modules, outputting the result to files and console. Requires Node.js to run.

const umd = require('umd'); const fs = require('fs'); // Example 1: 'return' style module (default behavior) const myModuleSource = ` function greeter(name) { return 'Hello, ' + name; } greeter.version = '1.0.0'; return greeter; `; const umdOutput = umd('my-greeter', myModuleSource); console.log('// UMD wrapped "return style" module:'); console.log(umdOutput); fs.writeFileSync('my-greeter.umd.js', umdOutput); // Example 2: CommonJS style module (requires commonJS option) const myCommonJSModuleSource = ` exports.add = (a, b) => a + b; exports.subtract = (a, b) => a - b; `; const umdCJSOutput = umd('my-math-lib', myCommonJSModuleSource, { commonJS: true }); console.log('\n// UMD wrapped "CommonJS style" module:'); console.log(umdCJSOutput); fs.writeFileSync('my-math-lib.umd.js', umdCJSOutput); console.log('\nUMD modules generated: my-greeter.umd.js and my-math-lib.umd.js');
Debug
Known issues
gotchaBy default, `umd` expects module source code to `return` the module's export. If your source uses CommonJS `module.exports` or `exports.propertyName`, you must explicitly set the `commonJS: true` option.
fix
For CommonJS source, use `umd(name, source, { commonJS: true })`. For 'return style', ensure your code concludes with `return yourExportedValue;`.
affects: >=1.0.0
gotchaModule names passed to `umd` have strict character requirements (alphanumeric, $, _, not starting with a number). Invalid characters will be silently stripped or converted to camel case, potentially leading to unexpected global variable names.
fix
Ensure module names adhere to `/[a-zA-Z0-9$_]+/` patterns, typically camelCase (e.g., `myAwesomeModule`), to avoid unintended transformations.
affects: >=1.0.0
deprecatedUMD is a legacy module format. While it provides broad compatibility, its relevance has decreased with the widespread adoption of native ECMAScript Modules (ESM) in modern browsers and Node.js. Using UMD might introduce unnecessary boilerplate or complexity if your target environments are primarily ESM-compatible.
fix
Consider if ESM or modern bundlers are more appropriate for your module distribution, especially for new projects or targeting modern environments. Use UMD primarily for legacy support.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: module is not defined (or exports is not defined)
Attempted to wrap a CommonJS module's source code without setting the `commonJS: true` option in the `umd()` function call.
fix
Ensure that for CommonJS-style source, the `commonJS` option is set: `umd('your-module', yourCjsSource, { commonJS: true });`
Wrapped UMD module does not export anything useful (e.g., module variable is undefined in consuming environments)
The source code for a 'return style' module (the default `umd` expectation) does not conclude with a `return` statement exporting the desired value.
fix
Modify your module source to explicitly `return` the value you wish to export, for example: `function MyFunc() {...} return MyFunc;`
UMD global variable name differs from expected module name (e.g., `my-cool-module` becomes `myCoolModule`)
The `umd` package automatically converts the provided module `name` to camelCase and strips invalid characters to create a valid global variable name for the UMD wrapper.
fix
Be aware of the automatic camelCasing and character stripping. If you need a specific global name, ensure your input `name` parameter adheres to JavaScript variable naming conventions (e.g., 'myCoolModule' instead of 'my-cool-module') or inspect the generated UMD output to confirm the exact name.
Upgrade
Version history
3.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
umd — npm install umd · libregistry