amdefine: AMD Define for Node.js

raw JSON →
1.0.1 verified Tue Apr 21 auth: no javascript abandoned

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.

error ReferenceError: define is not defined
cause 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.
error ReferenceError: require is not defined
cause 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) { ... }).
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.
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.
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.
fix Maintain the exact basic structure of the conditional `define` snippet to ensure proper optimization and code stripping by RequireJS.
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.
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.
npm install amdefine
yarn add amdefine
pnpm add amdefine

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.