{"id":32238955,"url":"https://github.com/fixjs/define.js","last_synced_at":"2025-10-22T14:32:17.272Z","repository":{"id":22947572,"uuid":"26297045","full_name":"fixjs/define.js","owner":"fixjs","description":"A JavaScript module loader, an implementation of AMD (Asynchronous Module Definition).","archived":false,"fork":false,"pushed_at":"2019-05-24T08:37:45.000Z","size":747,"stargazers_count":134,"open_issues_count":11,"forks_count":11,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-09-18T22:16:48.341Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fixjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-07T00:46:50.000Z","updated_at":"2025-02-07T09:52:36.000Z","dependencies_parsed_at":"2022-08-30T15:31:13.991Z","dependency_job_id":null,"html_url":"https://github.com/fixjs/define.js","commit_stats":null,"previous_names":["fixjs/fixdefine.js"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/fixjs/define.js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fixjs%2Fdefine.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fixjs%2Fdefine.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fixjs%2Fdefine.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fixjs%2Fdefine.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fixjs","download_url":"https://codeload.github.com/fixjs/define.js/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fixjs%2Fdefine.js/sbom","scorecard":{"id":401887,"data":{"date":"2025-08-11","repo":{"name":"github.com/fixjs/define.js","commit":"56070ea12f4ebdbf9d1a543d18878c56d16f3e1f"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.8,"checks":[{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":0,"reason":"Found 1/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: npmCommand not pinned by hash: examples/define-module-mapping/build.sh:3","Warn: npmCommand not pinned by hash: examples/rjs-amd-optimizer/build.sh:3","Info:   0 out of   2 npmCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 1 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-18T20:15:42.561Z","repository_id":22947572,"created_at":"2025-08-18T20:15:42.562Z","updated_at":"2025-08-18T20:15:42.562Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280450249,"owners_count":26332851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-22T02:00:06.515Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-10-22T14:30:41.284Z","updated_at":"2025-10-22T14:32:17.264Z","avatar_url":"https://github.com/fixjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DefineJS\n[![Build Status](https://secure.travis-ci.org/fixjs/define.js.png?branch=master)](http://travis-ci.org/fixjs/define.js)\n[![Code Climate](https://codeclimate.com/github/fixjs/define.js/badges/gpa.svg)](https://codeclimate.com/github/fixjs/define.js)\n[![Download stats](https://img.shields.io/npm/dm/definejs.svg)](https://www.npmjs.com/package/definejs)\n\nDefineJS is a lightweight implementation of [AMD](https://github.com/amdjs/amdjs-api) module format.\n\n\u003e The Asynchronous Module Definition (AMD) API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded.\n\n## Note: DefineJS 0.3\nSorry for such a delay in manining DefineJS, by the end of this month the new version will be released ...\n\n## Latest on 0.2.9\n- DefineJS now allows passing an actual **es6 function generator** right to the promise chain:\n\n```javascript\ndefine.Promise.resolve(jQuery)\n  .then(function * ($) {\n    var db = {},\n      AsyncDB = yield require('asyncdb'),\n      pkg = yield $.getJSON('package.json');\n\n    //Not relevant but it is worth noting that AsyncDB is an async local data storage based on IndexedDB\n    AsyncDB.new('packages', db);\n\n    var pageContent = yield $.get(pkg.repository.url);\n\n    $(pageContent).appendTo('.container');\n\n    var versionEl = $('.container span.version');\n\n    return yield db.packages.insert({\n      name: pkg.name,\n      version: pkg.version\n    });\n  })\n  .then(function (packageId) {\n    //a totally private and dedicated scope which has all it needs a packageId\n    //...\n  });\n```\n\n- DefineJS offers a new helper function for generators:\n\n```javascript\nfunction * myFunc(collection){\n  var data = yield getData(collection);\n\n  //Do something\n\n  return data.app; \n}\n```\n\nNow to call the function and start the chain you could easily do:\n\n```javascript\nmyFunc.go('users')\n  .then(function(app){\n    //The app object comes from the myFunc's return value\n\n  })\n```\n\nCompared to a regular function `.go()` is similar To a `fn.call()` and to pass an array as the list of desired arguments similar to `fn.apply()` DefineJS offers another function named: `goWith()`:\n\n```javascript\nmyFunc.goWith(['users'])\n  .then(function(app){\n    //The app object comes from the myFunc's return value\n\n  })\n```\n\nBut if there is no need to pass any argument to the function:\n\n```javascript\nmyFunc\n  .goThen(function(app){\n    //The app object comes from the myFunc's return value\n    \n  })\n```\n\n- DefineJS is not just an AMD module loader, it provides you with the luxury of setting up your application's asynchronous lifecycle without having to write down a huge `require` block, you could code using either of the new styles that DefineJS offers. If you don't like using IIFEs or if you are tired of the Pyramid of Doom when dealing with callbacks:\n\n```javascript\nconfig.go()\n  .then(firstPhase)\n  .then(secondPhase)\n  .then(finalPhase)\n  .catch(lifecycleInterruption)\n  .done(theEnd);\n\nfunction * sameLifecycle() {\n  var message;\n  try {\n    var packageInfo = yield config.go();\n    var app = yield firstPhase.go(packageInfo);\n    var shimModule2 = yield secondPhase.go(app);\n    message = yield finalPhase.go(shimModule2);\n  } catch (err) {\n    message = yield lifecycleInterruption.go(err);\n  }\n  theEnd(message);\n}\n``` \n\nTake a thorough look at the two code block above. They both do the exact same thing without us needing to create IIFEs and using callbacks.\n\n# Features\nOther than regular AMD module pattern, DefineJS also offers couple of nonstandard but usefull modular coding patterns. To make it more readable and getting to know the new features once they get released here we have top down list of DefineJS features list.\n\n- [CommonJS/AMD Hybrid Format](#commonjsamd-hybrid-format): This hybrid format allows to write modules with a CommonJS similar syntax.\n- [ES6 generators](#es6-generators): which allows to write asynchronous lazy loaded modules in a synchronous looking way of coding.\n  - [**Open Discussion**](https://github.com/fixjs/define.js/issues/9): This feature is still in its early days so that it needs more feedback from JavaScript community. There is an open issue ([#9](https://github.com/fixjs/define.js/issues/9)) to discuss the feedbacks, feel free to drop a line and bring up your ideas regarding this feature.\n- [Promised Modules](#promised-modules): Using the same AMD module style you can have privileged promise based modules.\n  - [**Open Discussion**](https://github.com/fixjs/define.js/issues/4): **Rejection state** of promised modules is still one of the open discussions, there is an open issue ([#4](https://github.com/fixjs/define.js/issues/4)) to discuss it, feel free to drop your comments.\n- [use() vs require()](#use-vs-require): another nonstandard function called `use()` with a similar approach to the standard `require()` function  which allows to have partial execution code blocks without having to use different main files.\n- [AMD Module format](#amd-module-format)\n\n## CommonJS/AMD Hybrid Format\nThis hybrid syntax allows to write modules with a new syntax similar to CommonJS. This feature is now possible thanks to the ES6 generators.\n\nLet's imagine a CommonJS module like:\n```javascript\n//app.js\nvar utils = require('utils'),\n  $ = require('../vendor/jquery');\n\nvar app = {\n  //...\n};\n\nmodule.exports = app;\n```\n\nThe DefineJS alternative is:\n\n```javascript\n//app.js\ndefine(function* (exports, module) {\n  var utils = yield require('utils'),\n    $ = yield require('../vendor/jquery');\n\n  var app = {\n    //...\n  };\n\n  module.exports = app;\n});\n```\n\nAs mentioned the new syntax is similar to the CommonJS coding style, with two specific differences. First the `yield` keyword and the next is the `define` wrapper with a `ES6 function generator`.\n\n## ES6 generators\nThis library provides you with a the possiblity of using ES6 generators and the `yield` keyword along with promises. You can use `yield` keyword to load your desired dependencies without getting through the callback hell.\n```javascript\n//app.js\ndefine(function* () {\n  var _,\n    app;\n  \n  if(loadashIsNeeded){\n    _ = yield require('../vendor/lodash');\n  } else {\n    _ = yield require('../vendor/underscore');\n  }\n  \n  app = {\n    //...\n  };\n  \n  return app;\n});\n```\nThen in order to require this module, you could `require` it as a regular AMD module:\n```javascript\n//main.js\nrequire(['app'],\n  function (app) {\n    app.lunch();\n  });\n```\nOr use the new `require` function like:\n```javascript\n//main.js\nrequire(function* () {\n  var app = yield require('app');\n  app.lunch();\n});\n```\nGive that a try and let us know how it feels to implement an asynchronous module definition with a fully synchronous looking code.\n\n## Promised Modules\nUsing the same AMD module style you can have privileged promise based modules. \nAll you need to do is just returning a promise in your modules, to make them promised modules. \nTo see how it works, just check out the [simple-promised-module](https://github.com/fixjs/define.js/tree/master/examples/simple-promised-module) example in the examples folder.\n\nIn this example we have a promised module named: [promisedModule.js](https://github.com/fixjs/define.js/blob/master/examples/simple-promised-module/promisedModule.js)\nwhich is responsible to wait for a specific global variable, then serve it as part of module's promised value.\n```javascript\n//promisedModule.js\ndefine([ /*'dependency'*/ ], function ( /*dependency*/ ) {\n\n  return new Promise(function (fulfill, reject) {\n    //Here you expect to have a global variable named: myApp after 2 seconds\n    //otherwise your module definition gets rejected\n\n    setTimeout(function () {\n      if (window.myApp !== undefined) {\n\n        //fulfill when succeeded and pass the fulfillment value\n        fulfill({\n          app: window.myApp,\n          log: 'This is just a sample promised object to serve as a promised module!'\n        });\n\n      } else {\n\n        //reject in case of error or unsuccessful operations\n        reject(new Error('No global myApp object found!!'));\n      }\n\n    }, 2000);\n  });\n\n});\n```\nNow you could easily require it, or add it as a dependency. What will happen is, it waits for your promise to get resolved then you will have the promised module object.\n```javascript\n//main.js\nrequire(['promisedModule'],\n  function(promisedModule){\n    console.log(promisedModule.log);//=\u003eThis is just a sample promised object!\n    console.log(promisedModule.app);\n  });\n```\n**Note**: we are still discussing about the proper way of handling the rejected state of a promised module. Any feedback or proposal is really appreciated.\n\n## use() vs require()\nYou can also have the same modules flow using a new offered syntax by DefineJS:\n```javascript\nuse(['dependency1', 'dependency2'])\n  .then(function(dependency1, dependency2){\n    //...\n    return dependency1.util;\n  })\n  .then(function(util){\n    //...\n    //use util object if it has any useful functionality\n    return util.map([/*...*/]);\n  })\n  .catch(function(e){\n    //in case of having a rejected promised module or any async error\n    console.error(e);\n  });\n```\n\n### AMD Module format\nYou can `define` and `require` your modules using the regular AMD format:\n```javascript\nmyGlobal.define([/*'dependency'*/], function(/*dependency*/]){\n  function moduleFunction(){\n    //...\n  }\n  return moduleFunction;\n});\n```\n```javascript\nmyGlobal.require([/*'moduleName'*/], function(/*moduleName*/]){\n  \n});\n```\n### Global define and require functions\nTo use AMD module definition functions(define and require) like what you have seen so far, as global functions, you could simply add the script tag like:\n```html\n\u003cscript global=\"window\" src=\"define.js\"\u003e\u003c/script\u003e\n```\nThen it could load any standard amd modules in your page.\n\n## Installation\n\nInstall with [Bower](http://bower.io):\n\n```\nbower install --save definejs\n```\n\nThe component can be used as a Common JS module, an AMD module, or a global.\n\n## API\nTo use DefineJS in your JavaScript code, you could simply add it as a script tag:\n```html\n\u003cscript src=\"define.js\"\u003e\u003c/script\u003e\n```\nThen you should call the definejs function to expose the amd modules functions to your desired global object:\n```javascript\ndefinejs(myGlobal);\n```\nThe easier way of achieving this, is to pass your desired global object to the `global` attribute of the script tag:\n```html\n\u003cscript global=\"myGlobal\" src=\"define.js\"\u003e\u003c/script\u003e\n```\nOr in case you need define and require functions as globals:\n```html\n\u003cscript global=\"window\" src=\"define.js\"\u003e\u003c/script\u003e\n```\nBased on the known JavaScript bad practice when defining global objects, this way with explicitly assigning the AMD functions to a specific global object or to the global scope you could be aware of the state of your global scope and also the possible consequences.\n\n**Note**:\n- **define.promise.js**: To be able to use the latest DefineJS feature, which allows to use ES6 generators, instead of `define.js` you should add `define.promise.js` to your page:\n    ```html\n    \u003cscript global=\"window\" src=\"define.promise.js\"\u003e\u003c/script\u003e\n    ```\n    The other parts are exactly the same.\n\n- **Promises polyfill**: DefineJS doesn't reinvent the wheel but provides you with the official **Promises polyfill**s from [promisejs.org](https://www.promisejs.org/). You could find the latest version of the polyfill in the [polyfills](https://github.com/fixjs/define.js/tree/master/polyfills) folder.\n\n## Testing\n\nInstall [Node](http://nodejs.org) (comes with npm) and Bower.\n\nFrom the repo root, install the project's development dependencies:\n\n```\nnpm install\nbower install\n```\n\nTesting relies on the Karma test-runner. If you'd like to use Karma to\nautomatically watch and re-run the test file during development, it's easiest\nto globally install Karma and run it from the CLI.\n\n```\nnpm install -g karma\nkarma start\n```\n\nTo run the tests in Firefox, just once, as CI would:\n\n```\nnpm test\n```\n\n\n## Browser support\n\n* Google Chrome (latest)\n* Opera (latest)\n* Firefox 4+\n* Safari 5+\n* Internet Explorer 8+\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffixjs%2Fdefine.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffixjs%2Fdefine.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffixjs%2Fdefine.js/lists"}