Import and export modules
- Imports are URLs or file system paths.
- export allows you to specify which parts of your module are accessible to users who import your module.
It adopts browser-like module resolution, meaning that file names must be specified in full. You may not omit the file extension and there is no special handling of index.js
.
Dependencies are also imported directly, there is no package management overhead. Local modules are imported in exactly the same way as remote modules. As the examples show below, the same functionality can be produced in the same way with local or remote modules.
Command:
In the local import example above an add
and multiply
method are imported from a locally stored arithmetic module. The same functionality can be created by importing add
and multiply
methods from a remote module too.
Command:
In the local import example above the add
and multiply
functions are imported from a locally stored arithmetic module. To make this possible the functions stored in the arithmetic module must be exported.
All functions, classes, constants and variables which need to be accessible inside external modules must be exported. Either by prepending them with the export
keyword or including them in an export statement at the bottom of the file.