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 🙏

© 2026 – Pkg Stats / Ryan Hefner

node2umd

v1.0.2

Published

A nothing-fancy tool to convert node-style module files into UMD module files.

Downloads

18

Readme

node2umd is a nothing-fancy tool to convert node-style module files into UMD module files.

It will convert your Node.js-style module file(s) into a UMD-style format that will work with Node.js, AMD-compatible module loaders (e.g. RequireJS), or loaded with a standalone HTML <script>-tag.

Installation

npm install node2umd [-g]

Usage & API

node2umd my-node-module.js > my-node-module.umd.js

Running that command converts a Node.js-style module file named my-node-module.js to a UMD-style module file named my-node-module.umd.js.

To use it programmatically, simply use the function it defines. For example, to do the same thing that executing it as a shell command does, you could write:

var node2umd = require('node2umd');

node2umd('my-node-module.js');

Its function signature is:

node2umd(input, [ { output: output } ])

  • input can be either a string -- in which case it's treated as the path to the module file you want to convert -- or a stream.Readable.
  • output (optional, default: process.stdout) should be a stream.Writeable

Node.js spec extension

module.globalName

node2umd respects a non-standard module property; module.globalName.

It uses its value, which should be a string, as the name of the global variable defined if a module is loaded with a standalone HTML <script>-tag. In any other module loading context it should be (and is, as far as node2umd is concerned) ignored. globalName is also added as a property on the value of the global variable. For example, if you define your module like:

module.exports = ...; // your module's implementation
module.globalName = 'MyModule';

and then pass it through node2umd to create my-module.umd.js, which you then use like:

<html>
	<head>
		<script src="my-module.umd.js" type="text/javascript"></script>
		<script type="text/javascript">
			document.body.appendChild(
				document.createTextNode(MyModule.globalName));
		</script>
	</head>
	<body>
		The name of my module is: 
	</body>
</html>

then you will have a page that looks like:

The name of my module is: MyModule

How it works

node2umd is a simple tool that does one, very simple thing.

It doesn't try to figure out what other modules or packages your code depends on, nor determine whether or not the input you give it is a valid Node.js module -- nor does it even try to parse your code at all. It doesn't depend on anything itself, besides some parts of the standard Node.js API.

Given a Node.js-style module file, it will output the contents of that file to stdout, prefixed with this text:

/* BEGIN node2umd PREFIX */
;(function(define) { define(function(require, exports, module) {
/* END node2umd PREFIX */

and ending with this text:

/* BEGIN node2umd POSTFIX */
}); })(typeof define === 'function' && define.amd ? define : function(factory) {
  var isNode,
      exportsObj = (isNode = typeof exports === 'object') ? exports : {},
      moduleObj = isNode ? module : { exports: exportsObj };

  var requireFn = isNode ? require : function(dependency) {
    return this[dependency];
  };

  var def = factory(requireFn, exportsObj, moduleObj) || moduleObj.exports;

  if (!isNode) {
    this[def.globalName] = def;
  }
});
/* END node2umd POSTFIX */

...and that's it.

In fact, to demonstrate just how simple it is; here's the full implementation of its main module file:

#!/usr/bin/env node

var fs = require('fs'),
    stream = require('stream');

function node2umd(input, options) {
  var output = (options = options || {}).output || process.stdout,
      reader =
          input instanceof stream.Readable ? input : fs.createReadStream(input);

  output.write(fs.readFileSync(__dirname + '/prefix.frag', {
    encoding: 'utf8'
  }));

  reader.on('end', function() {
    output.write(fs.readFileSync(__dirname + '/postfix.frag', {
      encoding: 'utf8'
    }));
  });

  reader.pipe(output);
}

if (require.main === module && process.argv[2] !== undefined) {
  node2umd(process.argv[2]);
}

module.exports = node2umd;