# EcmaScript Modules
EcmaScript modules are the official module format for the JavaScript language. ESM
are supported by ES4X by using
one of the two options:
- The initial script has extension
.mjs
- The initial script is prefixed with:
mjs:
# Initial Script
For the naked eye, the initial script is not that different from the commonjs script, for example index.mjs
:
import { Router } from '@vertx/web';
import { someRoute } from './routes';
const app = Router.router(vertx);
app.route('/').handler(someRoute);
vertx.createHttpServer()
.requestHandler(app)
.listen(8080);
In this case the someRoute
is imported from the routes.mjs
file:
export function someRoute(ctx) {
ctx.response()
.end('Hello from ES4X!');
}
# Compatibility
For compatibility reasons, you may have noticed that the import
statement in the initial script, does not include an
extension:
import { Router } from '@vertx/web';
import { someRoute } from './routes';
// ...
This is a small divergence from the official spec where ES4X loader will lookup modules as follows:
- Look up the exact file name:
./routes
- Look up with
.mjs
suffix:./routes.mjs
- Look up with
.js
suffix:./routes.js
WARNING
When working with ESM
the require()
is not available!
# Download Modules
Downloading modules at runtime is also possible. This feature is not ES4X
specific. In fact it just relies on the
official module loader from GraalJS
. Importing such modules is as simple as:
import { VertxOptions } from 'https://unpkg.io/@vertx/[email protected]/mod.mjs';
There are a few rules to know:
- HTTP modules will not be downloaded if there is not security manager in place.
- If such module has a
maven
counter part module, that is will NOT be downloaded. - Downloading executable code at runtime can be a security issue.
There could be cases where this could be useful, for example, to avoid a dependency on npm
when code is not public.
WARNING
Downloaded modules will not process any dependencies or maven counter parts.