Home > Mobile >  How to use import-map with Deno
How to use import-map with Deno

Time:12-24

I have this import_map.json file:

{
  "imports": {
    "node_modules/" : "./node_modules"
  }
}

at a high-level I am trying to create some compatibility for .ts files, for both Deno and Node.

My imports look like this:

import * as util from 'util';
import chalk from "node_modules/chalk";

When I run this:

deno run --import-map='import_map.json' ./src/linked-queue.ts

I get this loathsome error:

Import map diagnostics:
  - Invalid target address "file:///.../linked-queue/node_modules" for package specifier "node_modules/". Package address targets must end with "/".
error: Blocked by null entry for ""node_modules/""
    at file:///.../linked-queue/src/linked-queue.ts:4:19

Anyone know how to resolve this error?

CodePudding user response:

  "imports": {
    "node_modules/" : "./node_modules/"
  }

Add a trailing slash on the target specifier. See also the spec and the source.

CodePudding user response:

The manual covers this scenario in the following three sections:

I'll show a reproducible example rather than copy paste everything from the docs (because a few copied snippets aren't really enough; this is a multi-faceted issue) — however take note of the values in the import map, as they are derived by reading through all three linked sections of the documentation:

./import_map.json:

{
  "imports": {
    "chalk": "npm:[email protected]",
    "node:util": "https://deno.land/[email protected]/node/util.ts"
  }
}

./deno.jsonc:

{
  "importMap": "./import_map.json",
  "tasks": {
    // I included these permissions (which are required by chalk) in advance to avoid needing to grant them one-by-one at runtime:
    "dev": "deno run --allow-env=FORCE_COLOR,TF_BUILD,TERM,CI,TEAMCITY_VERSION,COLORTERM,TERM_PROGRAM,TERM_PROGRAM_VERSION src/linked-queue.ts"
  }
}

./src/linked-queue.ts:

import * as util from "node:util";
import chalk from "chalk";

console.log('util:', typeof util); // util: object
console.log('chalk:', typeof chalk); // chalk: function

Running in the terminal using the defined task:

% deno --version
deno 1.29.1 (release, x86_64-apple-darwin)
v8 10.9.194.5
typescript 4.9.4

% deno task dev
Task dev deno run --allow-env=FORCE_COLOR,TF_BUILD,TERM,CI,TEAMCITY_VERSION,COLORTERM,TERM_PROGRAM,TERM_PROGRAM_VERSION src/linked-queue.ts
util: object
chalk: function

% echo $?
0

So far, everything is great in Deno.


Let's check to see that the same code works without modification in Node.js. The following files need to be added to compile and run using Node, since it doesn't include all of Deno's built-in tooling:

./package.json:

{
  "name": "so-74905332",
  "version": "0.1.0",
  "type": "module",
  "scripts": {
    "compile": "tsc",
    "dev": "tsc && node src/linked-queue.js"
  },
  "license": "MIT",
  "dependencies": {
    "chalk": "5.2.0"
  },
  "devDependencies": {
    "@types/node": "^18.11.17",
    "typescript": "^4.9.4"
  }
}

./tsconfig.json:

Why these values? I'm just using a recommended base, linked to from the TS repo wiki:

// This file was autogenerated by a script
// Equivalent to a config of: strictest extends esm extends node18
{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Node LTS   ESM   Strictest",
  "_version": "18.12.1",
  "compilerOptions": {
    "lib": [
      "es2022"
    ],
    "module": "es2022",
    "target": "es2022",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "allowUnusedLabels": false,
    "allowUnreachableCode": false,
    "exactOptionalPropertyTypes": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "importsNotUsedAsValues": "error",
    "checkJs": true
  }
}

Running in the terminal using the defined npm script:

% node --version
v18.12.1

% npm install

added 3 packages, and audited 4 packages in 1s

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities

% npm run dev

> [email protected] dev
> tsc && node src/linked-queue.js

util: object
chalk: function

% echo $?
0

The same module source code also works in Node.js.

  • Related