Write JavaScript. Convert it into AMD.
Last updated 5 years ago by shovon .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install connect-commonjs-amd 
SYNC missed versions from official npm registry.


A connect middleware, to help you write front-end JavaScript in CommonJS, but run it as AMD modules.


It's a simple npm module, so you can either install it using npm,

$ npm install connect-commonjs-amd

or, you can add it to your package.json file.


This middleware works by determining if a requested file even exists. If it doesn't, then it will look for it in another directory that you specify.

I am going to assume that you have all your source JavaScript file in a ./src directory, and you want the outputed JavaScript to be stord in the ./public directory.

var express = require('express')
  , path = require('path')
  , app = express()
  , publicFolder = path.join(__dirname, 'public');

    src: path.join(__dirname, 'src')
  , dest: path.join(__dirname, 'public')



console.log("Server listening on port 3000");

So everytime a user requests a js/script.js, and the middleware isn't able to find it in ./public/js, then it will look in ./src/js. If it finds it there, it will then wrap the JavaScript with a define call, and then save it in ./public/js.

If you want to try it out, here's how. I'm going to assume that your source tree looks like so.

  • project-directory/
    • server.js (the above server code)
    • package.json
    • public/
      • index.html
      • js/
        • require.js (assuming that you use RequireJS as the AMD loader)
    • src/
      • app/
        • foo.js
        • script.js

Here's what your public/index.html might look like.

<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <title>My App</title>
  <script type="text/javascript" src="js/require.js"></script>
  <script type="text/javascript">
  require(['app/script'], function () {
    var script = require('script');

And perhaps your public/src/app/script.js might look like this.

var foo = require('app/foo');

module.exports.init = function () {

And your public/src/app/foo.js might look like this.

module.exports.greeting = "Hello, World!";

Start your server and go to your browser. You should see an alert box show with the string "Hello, World!".

You can find a slightly modified version of the above example, here.



connect-commonjs-amd has one function that you can call without using the module as a middleware. toCommonjs is that function.

var connectCommonJsAmd = require('connect-commonjs-amd')
  , sourceCode = 'console.log("Hello, World!");'


What about CoffeeScript?

You can use this module's toCommonJs function, in conjunction with the connect-coffee-script module.

var express = require('express')
  , path = require('path')
  , connectCommonJsAmd = require('connect-commonjs-amd')
  , port = process.argv[2] || 3000
  , publicFolder = path.join(__dirname, 'public')
  , server = express()
  , srcFolder = path.join(__dirname, 'src');

server.configure(function() {

  // Convert all your CommonJS CoffeeScript to AMD.
      src: srcFolder
    , dest: publicFolder
    , compile: function (str, options) {
      options.bare = true;
      code = CoffeeScript.compile(str, options);
      // Here's the call to `toCommonJs`.
      return connectCommonJsAmd.toCommonJs(code);

    dumpException: true,
    showStack: true


console.log("Server listening on port " + port);



Mocha is used for the testing suite. And, you can run the test using the following command.

$ npm test

Or you can use the mocha command. Both the same thing.

Current Tags

  • 0.0.4                                ...           latest (5 years ago)

4 Versions

  • 0.0.4                                ...           5 years ago
  • 0.0.3                                ...           7 years ago
  • 0.0.2                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 1
Dependencies (4)
Dev Dependencies (3)
Dependents (0)

Copyright 2014 - 2016 © |