import filename
Imports exported functions and values from another JavaScript file (a module), so they can be used in the current file.
Supported forms:
- Named import:
import { formatPrice, currency as cur } from "#/Lib/helpers.js"; - Namespace import:
import * as helpers from "#/Lib/helpers.js"; - Dynamic import:
import("#/Lib/helpers.js").then(lib => { … });
Not supported: default import (import x from "…") and the old form import "path", which pasted the file in and now gives a compile error. Export what you need with export function / export const in the module and use a named or namespace import.
A module runs once per request, even when several files import it; module-level variables are shared within that request, never between requests. A circular import is an error. Modules can be imported from #/API files, services and .hash pages, and run in the same mode as the file that imports them.
Parameters
filenamestringrequiredPath to the module file, resolved from the site root, for example "#/Lib/helpers.js".
Returns
Nothing is returned. The imported bindings (functions, objects or values) are made available in the current scope.
Example
// #/Lib/helpers.js contains:
// export function formatPrice(n) { return n.toFixed(2) + " kr"; }
// export const currency = "NOK";
// Named import - bring specific exports into scope
import { formatPrice, currency as cur } from "#/Lib/helpers.js";
// Namespace import - bring everything in under one object
import * as helpers from "#/Lib/helpers.js";
// Use them inside an API function
export default () => {
return {
price: formatPrice(199),
currency: cur,
same: helpers.currency
};
}