amdefine: AMD Define for Node.js
raw JSON →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.
Common errors
error ReferenceError: define is not defined ↓
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. error ReferenceError: require is not defined ↓
define(function (require) { ... }) to get require as an argument, or include 'require' in your dependency array: define(['require', 'dependency'], function(require, dependency) { ... }). Warnings
gotcha Using `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. ↓
gotcha The 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. ↓
deprecated The 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. ↓
Install
npm install amdefine yarn add amdefine pnpm add amdefine Imports
- define wrong
import { define } from 'amdefine'correctif (typeof define !== 'function') { var define = require('amdefine')(module) } - amdefine/intercept wrong
import 'amdefine/intercept'correctrequire('amdefine/intercept') - require (inside define) wrong
define(['dependency'], function () { var dep = require('dependency'); });correctdefine(function (require) { var dep = require('dependency'); });
Quickstart
// --- 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.