Introduction
Import maps help you use simple names instead of long URLs when importing code. This makes your code easier to read and manage.
Import maps help you use simple names instead of long URLs when importing code. This makes your code easier to read and manage.
{
"imports": {
"alias/": "https://example.com/path/"
}
}lodash to the full URL of the lodash library.{
"imports": {
"lodash": "https://cdn.skypack.dev/lodash"
}
}utils/ to a local folder ./src/utils/.{
"imports": {
"utils/": "./src/utils/"
}
}This example shows an import map that maps fmt/ to Deno's standard formatting library URL. The code imports bold from fmt/colors.ts using the alias, making the import cleaner.
// import_map.json
{
"imports": {
"fmt/": "https://deno.land/std@0.203.0/fmt/"
}
}
// main.ts
import { bold } from "fmt/colors.ts";
console.log(bold("Hello, Deno import maps!"));Remember to run Deno with the --import-map=import_map.json flag to use the import map.
Import maps only affect module specifiers in your code, not dynamic imports or other URLs.
Import maps let you use short names instead of long URLs for imports.
They make your code cleaner and easier to update.
Use the --import-map flag to enable them in Deno.
import maps in Deno?imports.json?--import-map=.--import-map=imports.json, which is the correct form.imports.json file:{
"imports": {
"lodash": "https://cdn.skypack.dev/lodash"
}
}import _ from "lodash"; console.log(typeof _);
--import-map=imports.json?typeof _ returns "object".imports.json:{
"imports": {
"react": "https://cdn.skypack.dev/react"
}
}deno run --import-map=imports.json app.ts gives an error: Cannot resolve module "react". What is the likely cause?lodash and dayjs, with import maps to keep your code clean. Your imports.json is:{
"imports": {
"lodash": "https://cdn.skypack.dev/lodash",
"dayjs": "https://cdn.skypack.dev/dayjs"
}
}import _ from "lodash"; import dayjs from "dayjs"; console.log(dayjs().format());
--import-map flag pointing to your map file.Pass the test and this pattern moves unknown