{"id":15955,"library":"amdefine","title":"amdefine: AMD Define for Node.js","description":"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.","status":"abandoned","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/jrburke/amdefine","tags":["javascript"],"install":[{"cmd":"npm install amdefine","lang":"bash","label":"npm"},{"cmd":"yarn add amdefine","lang":"bash","label":"yarn"},{"cmd":"pnpm add amdefine","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Runtime dependency for projects that want to use AMD's define() API in a Node.js environment.","package":"amdefine","optional":false}],"imports":[{"note":"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.","wrong":"import { define } from 'amdefine'","symbol":"define","correct":"if (typeof define !== 'function') { var define = require('amdefine')(module) }"},{"note":"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.","wrong":"import 'amdefine/intercept'","symbol":"amdefine/intercept","correct":"require('amdefine/intercept')"},{"note":"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.","wrong":"define(['dependency'], function () { var dep = require('dependency'); });","symbol":"require (inside define)","correct":"define(function (require) { var dep = require('dependency'); });"}],"quickstart":{"code":"// --- package.json (excerpt) ---\n// {\n//   \"dependencies\": {\n//     \"amdefine\": \">=0.1.0\"\n//   }\n// }\n\n// --- my-amd-module.js ---\nif (typeof define !== 'function') {\n  var define = require('amdefine')(module);\n}\n\ndefine(['./my-other-module'], function (myOtherModule) {\n  'use strict';\n  console.log('Inside my-amd-module.js');\n  \n  function greet(name) {\n    return myOtherModule.sayHello(name) + ' from amdefine module.';\n  }\n\n  return {\n    greet: greet,\n  };\n});\n\n// --- my-other-module.js ---\nif (typeof define !== 'function') {\n  var define = require('amdefine')(module);\n}\n\ndefine(function () {\n  'use strict';\n  console.log('Inside my-other-module.js');\n\n  function sayHello(name) {\n    return 'Hello, ' + name;\n  }\n\n  return {\n    sayHello: sayHello,\n  };\n});\n\n// --- app.js (main entry point) ---\n// Ensure 'npm install' has been run to get amdefine\nconst myAmdModule = require('./my-amd-module');\n\nconsole.log('Running app.js');\nconsole.log(myAmdModule.greet('World'));\n\n// Expected console output:\n// Inside my-other-module.js\n// Inside my-amd-module.js\n// Running app.js\n// Hello, World from amdefine module.","lang":"javascript","description":"Demonstrates defining and consuming AMD modules using `amdefine` in a Node.js application, including the conditional `define` snippet and inter-module dependencies."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Maintain the exact basic structure of the conditional `define` snippet to ensure proper optimization and code stripping by RequireJS.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"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.","message":"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.","severity":"deprecated","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","cause":"The `define` function provided by `amdefine` was not correctly loaded or made available in the module's scope.","error":"ReferenceError: define is not defined"},{"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) { ... })`.","cause":"You are attempting to use `require()` directly inside an AMD `define` factory function without it being explicitly provided.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}