amdefine: AMD Define for Node.js

1.0.1 · abandoned · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

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.

view raw JSON →