Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support for npm installed packages on the browser #846

Closed
brettz9 opened this Issue · 3 comments

2 participants

@brettz9

Npm does not restrict itself to Node.js or Node.js only modules, so it can be useful for sharing modules for the browser (though it has what seems to me to be an unnecessary restriction against upper-case characters in module names). Might requirejs either allow:

  1. Full Ajax checks for package.json and resolution of the file referenced in its "main" property.
  2. Expect a convention such as /node_modules/<pkg>/<pkg>.js or /node_modules//index.js, or /node_modules/<pkg>/main.js so that Ajax can be avoided. (Or maybe fallback to the Ajax if it is not found.)

Of course, it would be ideal if the same code written for the browser in this manner could still work in Node as well.

@jrburke
Owner

It is best if either the package manager or a command line helper tool either creates an adapter module so that 1) 'example.js' points to the 'main' module in the 'example' directory, or 2) enters a paths or packages config in a requirejs config block. It could then also convert the modules to amd format for example too.

volo does path 1) create an adapter module. If the project that volo installs into is an AMD project, it will also try to convert any node/cjs modules to amd format.

Unfortunately, npm as-is lays out code that is not very helpful for dynamic loading for the browser.

A loader in the browser should also not try to do http requests for a package.json as that will lead to 404s in some cases, and it is extra requests that can be avoided by laying out the files and directories, and creating an adapter module once. JSON fetching is also subject to CORS rules, which leads to support requests.

So no plans to add functionality in this area, as the right solution is better command line tools for project setup.

The "same module system in node and browser" will likely have to wait until ECMAScript modules land.

@jrburke jrburke closed this
@brettz9

As far as ES6 modules and command line helpers, this looks interesting: https://github.com/umdjs/es6-module-transpiler .

Despite this, I still think it would be useful to have something for on-the-fly loading, as it is very valuable to debug without an intermediate step of using the command line, even if just one command. I am therefore interested to take a closer look at/try out https://github.com/jspm/jspm-loader/ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.