"react" instead of full URLs) and remap module locations.
Import Maps in deno.json
The recommended way to define import maps is directly in yourdeno.json file using the imports field:
deno.json
Standalone Import Map Files
You can also use a separate import map file and reference it in your configuration:deno.json
import_map.json
If you specify both an
importMap field and imports/scopes in deno.json, the inline imports/scopes will take precedence and override the external import map.Path Mapping
Import maps can remap paths to make imports cleaner:deno.json
Scoped Imports
Thescopes field allows you to define import mappings that only apply within specific scopes:
deno.json
- Most code will resolve
"lodash"to the CommonJS version from npm - Code in the
./vendor/directory will resolve"lodash"to the ES modules version
Common Import Patterns
JSR Packages
deno.json
npm Packages
deno.json
Local Modules
deno.json
Mixed Sources
deno.json
Subpath Imports
You can map specific subpaths of packages:deno.json
Version Management
Import maps make it easy to manage versions centrally:deno.json
Compiler Options Integration
Import maps work alongside TypeScript’spaths compiler option, but serve different purposes:
deno.json
In Deno,
compilerOptions.paths is only applied to bare specifier entries and is primarily for TypeScript compatibility. Use imports for runtime module resolution.Vendoring Dependencies
You can use import maps to vendor external dependencies:Dynamic Imports
Import maps also work with dynamic imports:Command Line Override
You can specify an import map from the command line:importMap specified in deno.json.
Best Practices
Use JSR and npm prefixes
Always use
jsr: and npm: prefixes to make it clear where packages come from.Version pinning
Use exact versions in production, ranges in development.
Path mappings
Create consistent path aliases to avoid deep relative imports.
Centralized management
Keep all imports in
deno.json for easier maintenance.Real-World Example
Here’s a complete example from the Deno source code:deno.json
Troubleshooting
Import map not being used
Make sure yourdeno.json is in the project root, or specify it explicitly:
Conflicts between imports and scopes
Scoped imports take precedence over top-level imports for files within that scope.TypeScript errors
If TypeScript can’t resolve imports, ensure you have bothimports and appropriate compilerOptions.paths set up.