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 🙏

© 2025 – Pkg Stats / Ryan Hefner

common-js

v0.3.8

Published

module.exports and module.import for browsers too

Downloads

4,484

Readme

CommonJS + module.import() build status

This module aim is to bring both CommonJS like module behavior on Web browsers, and a promise based module.import(path) to both browsers and NodeJS.

Yes, it resolves paths relatively to the current one!

Yes, it is secure too, check the CSP enabled page!

Don't miss the introductory blog post about this idea!

Browser Example

<!doctype html>
<html>
  <script
    id="common-js"
    data-main="/js-browser/main.js"
    src="common.js"
  ></script>
</html>

Having a single script with id="common-js" is all it takes to be able to load asynchronously any other file or module.

The main entry point /js-browser/main.js will resolve relative paths from /js-browser/ folder.

Its loaded modules will resolve their own imported paths from where they've been loaded, and so on. The same goes for NodeJS, it's indeed same logic behind require.

// /js-browser/main.js loading /js-browser/test.js
module.import('./test').then(function (test) {
  test('Hello CommonJS!');
  // will output:
  // Hello CommonJS!
  // from /js-browser/test.js
});

// the /js-browser/test.js content
module.exports = function (message) {
  alert(message + '\nfrom ' + module.filename);
};

Load multiple modules at once

Promise.all([
  module.import('./a'),
  module.import('//cdn.something.com/cool.js'),
  module.import('../sw.js'),
  module.import('/root/too.js')
]).then(function (modules) {
  const [a, cool, sw, too] = modules;
});

Exporting modules asynchronously

// an async example of /js-browser/test.js content
// for the /js-browser/main.js file nothing changes
module.exports = new Promise(function (resolve) {
  setTimeout(
    resolve,
    1000,
    function (message) {
      alert(message + '\nfrom ' + module.filename);
    }
  );
});

Compatibility

You can test your target directly through the live test page. What I could test was the following:

Mobile Android 2+, iOS5+, WP7+, BBOS7+, FFOS1+, WebOS2+, Kindle Paper & Fire

Desktop Chrome, FF, Safari, Opera, IE9+ (theoretically IE8 too but it needs few polyfills upfront)

What else?

The synchronous require and both __filename and __dirname are also exposed, but nothing else from NodeJS core is available. You are responsible for loading all the modules you need, possibly only when you need them.

F.A.Q

  • Does it load every time? It uses a cache, like NodeJS does. If you load same module twice, even from different relative paths, it'll use the cached one.
  • Why on the module? There are scripts, script type module, importScripts, a dynamic import proposal, you name it ... this one actually works and it's backward compatible with modules that don't care about this solution existing.
  • Why not ES2015 modules? Because those, so far, never truly solved anything. Actually, ES6 modules created more problems due inability to require modules at runtime and/or on the browser.
  • Is there a CDN I can use to test? There is always one for npm modules. https://unpkg.com/common-js@latest should be already OK.
  • Is this using eval? No. It's using a technique that is even compatible with highest security standards such Content Security Policy

License

Copyright (C) 2017 by Andrea Giammarchi - @WebReflection