Registry / amdefine

amdefine

JSON →
library1.0.1jsnpmunverified

amdefine is a JavaScript module that allows the Asynchronous Module Definition (AMD) `define()` API to operate within a Node.js environment without requiring a full AMD loader. This enables developers to create modules using the AMD format, primarily designed for browser environments, and subsequently run them in Node.js applications. The package, currently at version 1.0.1, has not seen updates in approximately nine years and is considered abandoned. It provides a shim for `define` via a conditional `require` snippet placed at the top of each AMD module, or through an `amdefine/intercept` module that automatically injects the shim globally. While AMD is largely superseded by CommonJS and ES Modules for modern Node.js development, amdefine remains relevant for integrating or migrating existing AMD codebases into a Node.js context, acting as a compatibility layer with minimal overhead.

npm install amdefine
INSTALL
IMPORT
SIG · AMDEFINE
A
amdefine
javascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

define
if (typeof define !== 'function') { var define = require('amdefine')(module) }
import { define } from 'amdefine'
This exact conditional snippet must be placed at the top of each AMD module to ensure the `define` function is available when running in Node.js without a full AMD loader. Its precise structure is also critical for compatibility with the RequireJS optimizer's stripping behavior.
amdefine/intercept
require('amdefine/intercept')
import 'amdefine/intercept'
This module globally applies the `define` shim by overriding Node.js's `Module._extensions['.js']` handler. It should only be used in top-level Node.js *applications* (e.g., `index.js`, `server.js`), not within reusable libraries, due to its global side effects.
require (inside define)
define(function (require) { var dep = require('dependency'); });
define(['dependency'], function () { var dep = require('dependency'); });
When using the CommonJS-wrapped AMD style, `require` is passed as an argument to the factory function. If using the array-based dependency declaration, `require` must be explicitly listed in the dependency array (e.g., `define(['require', 'dependency'], function(require, dependency) { ... })`) to be available.

Demonstrates defining and consuming AMD modules using `amdefine` in a Node.js application, including the conditional `define` snippet and inter-module dependencies.

// --- package.json (excerpt) --- // { // "dependencies": { // "amdefine": ">=0.1.0" // } // } // --- my-amd-module.js --- if (typeof define !== 'function') { var define = require('amdefine')(module); } define(['./my-other-module'], function (myOtherModule) { 'use strict'; console.log('Inside my-amd-module.js'); function greet(name) { return myOtherModule.sayHello(name) + ' from amdefine module.'; } return { greet: greet, }; }); // --- my-other-module.js --- if (typeof define !== 'function') { var define = require('amdefine')(module); } define(function () { 'use strict'; console.log('Inside my-other-module.js'); function sayHello(name) { return 'Hello, ' + name; } return { sayHello: sayHello, }; }); // --- app.js (main entry point) --- // Ensure 'npm install' has been run to get amdefine const myAmdModule = require('./my-amd-module'); console.log('Running app.js'); console.log(myAmdModule.greet('World')); // Expected console output: // Inside my-other-module.js // Inside my-amd-module.js // Running app.js // Hello, World from amdefine module.
Debug
Known issues
gotchaUsing `amdefine/intercept` modifies Node.js's global module loading behavior by overriding `Module._extensions['.js']`. This can lead to unexpected side effects or conflicts with other modules that modify the same extension hook. It is explicitly discouraged for reusable library code.
fix
For library code or scenarios requiring more control, avoid `amdefine/intercept`. Instead, use the conditional `if (typeof define !== 'function') { var define = require('amdefine')(module) }` snippet at the top of each individual AMD module.
affects: >=0.1.0
gotchaThe specific structure of the conditional `define` snippet (`if (typeof define !== 'function') { var define = require('amdefine')(module) }`) is essential for compatibility with the RequireJS optimizer. Any significant deviation (e.g., adding extra statements within the `if` block) may prevent the optimizer from correctly stripping out the `amdefine` code for browser builds.
fix
Maintain the exact basic structure of the conditional `define` snippet to ensure proper optimization and code stripping by RequireJS.
affects: >=0.1.0
deprecatedThe AMD module format, while foundational, has largely been superseded by ES Modules (ESM) and CommonJS (CJS) for modern JavaScript development, especially in Node.js. `amdefine` itself has not been updated in many years, reflecting the declining relevance of AMD in the Node.js ecosystem.
fix
For new projects, consider using ES Modules or CommonJS for module definition. `amdefine` is best suited for maintaining or integrating existing AMD-based codebases within Node.js.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: define is not defined
The `define` function provided by `amdefine` was not correctly loaded or made available in the module's scope.
fix
Ensure the conditional snippet `if (typeof define !== 'function') { var define = require('amdefine')(module) }` is present and correctly placed at the very top of your AMD module file, or that `require('amdefine/intercept')` is executed early in your application's entry point.
ReferenceError: require is not defined
You are attempting to use `require()` directly inside an AMD `define` factory function without it being explicitly provided.
fix
Use the CommonJS-wrapped AMD format by declaring `define(function (require) { ... })` to get `require` as an argument, or include 'require' in your dependency array: `define(['require', 'dependency'], function(require, dependency) { ... })`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
amdefinerequiredRuntime dependency for projects that want to use AMD's define() API in a Node.js environment.
Agent activity
12 hits · last 30 days
node
10
Resources
amdefine — npm install amdefine · libregistry