#Let a loaded module import another? deno_core
2 messages · Page 1 of 1 (latest)
Yes, it is possible to import modules in Deno, including from within other modules. The documentation provides examples of both local and remote imports.
For local imports, you can import functions, classes, constants, and variables from a locally stored module. Here's an example from the documentation:
/**
* local.ts
*/
import { add, multiply } from "./arithmetic.ts";
For remote imports, you can import methods from a remote module. Here's an example from the documentation:
/**
* remote.ts
*/
import {
add,
multiply,
} from "https://x.nest.land/[email protected]/source/index.js";
In both cases, the imported functions, classes, constants, and variables must be exported from the module they're stored in. You can do this by adding the export keyword to the beginning of the function signature, class, constant, or variable.
However, if you're working with the deno_core crate in Rust and want to load a module that can import another module, you'll need to implement the deno_core::ModuleLoader trait for your module loader. This trait requires you to implement resolve and load functions. The resolve function resolves the module specifier, and the load function loads the module.
Here's an example from the documentation:
// main.rs
struct TsModuleLoader;
impl deno_core::ModuleLoader for TsModuleLoader {
fn resolve(
&self,
specifier: &str,
referrer: &str,
_kind: deno_core::ResolutionKind,
) -> Result<deno_core::ModuleSpecifier, deno_core::error::AnyError> {
deno_core::resolve_import(specifier, referrer).map_err(|e| e.into())
}
}
In this example, the resolve function is calling deno_core::resolve_import to resolve the module specifier.
Please note that this information is based on the