include 'foo/index.js' or include 'foo'?

# monolithed (10 years ago)

I could not find an answer in the specification regarding the following cases:

import './foo/index.js'
import 'foo/index.js'
import 'foo/index'
import 'foo'
import 'foo/'

Is there a difference?

Node.js lets create an 'index.js' file, which indicates the main include file for a directory. So if you call require('./foo'), both a 'foo.js' file as well as an 'foo/index.js' file will be considered, this goes for non-relative includes as well.

# monolithed (10 years ago)

import './foo/index.js';

import 'foo/index.js';

import 'foo/index';

import 'foo';

import 'foo/‘;

# Glen Huang (10 years ago)

I believe this is out the scope of ecmascript. It’s up to the host to determine how the paths are resolved.

See people.mozilla.org/~jorendorff/es6-draft.html#sec-hostnormalizemodulename, people.mozilla.org/~jorendorff/es6-draft.html#sec-hostnormalizemodulename

# John Barton (10 years ago)

The following solution has worked very well for us:

import './foo/index.js';

means resolve './foo/index.js' relative to the importing file.

All of the rest mean look up 'foo' in the developer's mapping of names, replacing 'foo' with a path that is then used to resolve the import.

To be sure 'foo' 'foo/index' and 'foo/' would likely fail after lookup since they don't name files.

(This kind of thing cannot be "up to the host". If TC39 passes on deciding, then developers will).

# Isiah Meadows (10 years ago)

The current spec being worked on to resolve this problem is at whatwg.github.io/loader. It's still under construction, but it's being written with browser and Node interop in mind.

# Isiah Meadows (10 years ago)
  1. I made a mistake sending the email initially.
  2. I was referencing the spec for the future module loader.
  3. I think you meant to hit "Reply All".