App root module specifiers

# PETER Flannery (7 years ago)

Currently there isn't a way to import a module using an app root module specifier.

I.E. import * as Stuff from 'utils/someUtil.js'

Relative paths suffices in the simplest applications but not being able to resolve from the root of the app leads to a complex stack of relative path entries I.E. ../../..

One example of where these complex stacks arise is importing source modules from within test modules where usually tests are defined in a test folder separated from the source.

Another issue arises when moving a file to another location which then leads to recalculating the relative path stack.

The idea I have is to have custom alias mappings which would be defined in a place where the host can find and resolve module specifier aliases I.E. an .es file in the app root containing a json map of aliases. The maps could possibly be auto generated using build tools when publishing an app.

# Darien Valentine (7 years ago)

I’m not entirely certain of this, but I don’t think that the semantics of resolving a module specifier string are themselves a facet of the ES spec. However, I think the issue you’re describing may already be addressed in the implementations so far (Safari 10.1, Chrome Canary, FF Nightly) because the paths are treated like any other URI and can therefore begin with "/".

So if we have a module at "www.host.foo/foo/foo.js" with the following content:

import bar from 'bar.js'
import baz from '/baz.js'

...then it looks for "www.host.foo/foo/bar.js" and "www.host.foo/baz.js".

I’m not sure if browsers are currently taking into account the <base>

element when doing this, but this is the behavior I’ve observed so far, and it seems consistent with how other resource linking works in the browser (e.g. imports in CSS files).

Node, which doesn't support import yet, maybe trained us a bit to see module specifiers a different way. I’m not sure what the solution will end up being there. The "/" is already taken to mean system root rather than package root I think, and paths without any slashes have special resolution behavior that's nothing like URIs.

# Jordan Harband (7 years ago)

Indeed, module loading isn't really part of the language spec.

I think that you may want to submit a node.js enhancement proposal at nodejs/node-eps - node would need to add something to both current require and upcoming import to support having a "package root" specifier; at which point transpilers and rewrite rules could convert to whatever browsers (which have no concept of "package" in the first place) need.