npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

amdrequire

v0.0.3

Published

Reuse require.js AMD modules in node.

Downloads

160

Readme

amdrequire

AMDrequire is NPM package that makes Node understand modules defined in AMD format. When using AMDrequire it is possible to define a module like this.

world.js

define([], function() {
	return "world";
});

And use it like an usual AMD module.

require(['world.js'], function(world){
	console.log('Hello ' + world); //Outputs a brand new "Hello world"
});

AMDrequire also respect node module definitions, so it is still possible to load node modules as usual

var moduleExports = require('module');

Making Node understand both formats let the developer reuse browse code in the server without modifications, basically require.js modules, and write modules directly to be used in both places.

License

Licensed under BSD-2. See license

How to install

Using npm you only need to type in the console

npm install amdrequire

How to use

AMDrequire overrides Node's require function to check whether a request of an AMD or Node module is happening, so it is recommended to require it in the first line of your application's main file.

require = require('amdrequire');

Once AMDrequire is initialized, the require function will understand AMD requires and defines in every module, no more set up needed.

How to configure

The configuration is really similar to the require.js one, the first require object has a config method that accepts a paths attribute to define the named requires:

require = require('amdrequire');
require.config({
	paths: {
		'world': 'somedir/world'
	}
	basePath: __dirname + '/public/assets/js',
	publicPath: __dirname + '/public'
});

Using the example above, when calling require(['world']) AMDrequire will look up the module in the file public/assets/js/somedir/world.js relative to the current dir. The config method is only available when AMDrequire is required for the first time, and not in other modules.

Let's have a look at the options of the config method:

paths

Defines some named requires, so to use those modules it is not needed to write their full path, just the name defined here. The paths are relative to the basePath attribute.

Note: This named requires only affects to AMD requires so you won't be able to call require('world') outside a ```define`` to get the world module.

basePath

The basePath option set the root directory for AMD requires. All the relative paths used in AMD's require and define calls, that don't start with ./ or ../ will be relative to this directory. It is equivalent to require.js baseUrl option, so it should point to the local path of that URL.

publicPath

Defines the path of the root URL directory. In the browser it is possible to require modules using root routes, they start with a slash / like /route/from/the/root. publicPath tells what is the equivalent to that / route to AMDrequire, so it can handle this kind of requires.

Some tips

Reusing require.js configuration

It is possible to reuse the configuration object used for require directly with AMDrequire, so you can define your module paths in one site. The easiest way is creating a module with that data:

config.js

define({
	path:{
		'hello': 'path/to/hello',
		'world': 'way/to/get/the/world'
	},
	baseUrl: '/js'
	// Some more require.js configuration
});

Yes, with require.js it is possible to define modules as objects using the object as the first parameter, and yes, AMDrequire knows about it, so you can use that module to config AMDrequire this way:

require = require('amdrequire');
require(['config.js'], function(config){
	// Set basepaths first
	config.basePath = __dirname + '/public/js';
	config.publicPath = __dirname + '/public';
	require.config(config);
});

Using AMD modules as Node modules

When an AMD module is required once, its exported value is stored in the cache like any other Node module, so you can require it using Node notation and the module path:

require(['world'], function(world){
	// ...do any stuff
});

var world = require('path/to/world');
console.log('Hello ' + world); // Will output "Hello world"

However, you can't require an AMD module using its name, unless you are inside a define function.


require(['world'], function(world){
	// ...do any stuff
});

require(__dirname + '/path/to/world'); // world
require('world'); // fail!!
define([], function(){
	// This will return the result if the 'world' module
	// has been required before.
	var world = require('world');
});

AMDrequire modules are not asynchronous

In case you don't know, AMD stands for Asynchronous Module Definition, but since AMDrequire is using Node's require function to get their modules, their fetching is actually synchronous. It is possible to make code like this:

var mundo;
require(['world'], function(world){
	mundo = world;
});
console.log('Hello ' + mundo);
// Outputs "Hello world" because the code inside the require
// callback is always executed before this line.

What's next?

AMD has just born and I am sure there are lots of ways of improve it. Feel free to fork it and make pull requests. These are some ideas.

Shims

require.js allows third party libraries to be used as modules exporting some variable as the return of the module. They are called shims and AMDrequire doesn't support them yet.

require protocol paths

require.js allows to load files using protocols, like 'http' or 'https'. AMDrequire doesn't support this feature and using protocols will lead to errors.

I am not sure if load external files is a good idea in Node apps, but I am sure that if protocol and domain matches our protocol and domain AMDrequire should work as if the require was local and it is not working like this yet.

I personally would recommend always using relative paths instead of complete URLs.

More testing

There is a small node app to test AMDrequire called amdrequire-test. It tests the different ways that require.js load the files, but I am sure that those tests can be improved. Also, testing possible Node and AMD require conflicts would be great.

Plugins

No plugins are yet supported. I haven't investigated about how to do it, but it would be great to give universal require.js plugin support. If complete plugin support is not possible, at least would be great to have text plugin support.

Named defines

AMDrequire doesn't support named defines as they appeare in the require.js docs. It shouldn't be difficult to support them.

References

This piece of code has been developed with one eye on the Node's module source code and the other on the require.js documentation.

If you want to know more about javascript modules implementation Addy Osmani's writing modular js article is a must.