ModuleSpecifier: include ".js" or not?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

ModuleSpecifier: include ".js" or not?

Axel Rauschmayer
In Node.js you can either mention the file extension ".js" or omit it when you require a module. Which one is preferred for ES6 ModuleSpecifiers?

Thanks!

Axel

-- 
Dr. Axel Rauschmayer
[hidden email]
rauschma.de




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ModuleSpecifier: include ".js" or not?

Wes Garland
Some food for thought from a non-standard use-case.

In GPSEE, we can require("module") - but we support both modules written in JavaScript, and modules written in C (technically, any compiled binary with the correct C API), or both. We dlload("module.so") (if present), then interpret "module.js" (if present) as part of the module loading procedure.

So, from my POV, loading a module from script with the extension .js is not ideal.  If you are going to assign meaning to the extension, you must think about what it means.  In our case, it indicates the file type to load, and the implementation of the library is not relevant to the library user - only the API matters.  And if you are only ever going to allow one extension type, then there is no point to typing .js on the end of every module name.

In my opinion, the ModuleSpecifier should be fed the module name, and is it up to the loader to munge the filename (eg by adding .js) as it sees fit.

Wes



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: ModuleSpecifier: include ".js" or not?

Alexander Guinness
In reply to this post by Axel Rauschmayer
This question has already been asked
https://mail.mozilla.org/pipermail/es-discuss/2015-February/041430.html


I prefer to follow the following rules:


#### File structure

```
root/
   foo/
       index.js

```

#### Export module

```js
   export default foo class () {}
```

#### Import module

```js
import foo from './foo';
```

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss