NodeJS 如何使用导入Map与Deno

5vf7fwbs  于 2022-12-26  发布在  Node.js
关注(0)|答案(2)|浏览(177)

我有这个import_map. json文件:

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

在高层次上,我试图为.ts文件创建一些兼容性,包括Deno和Node。
我的导入如下所示:

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

当我运行这个程序时:

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

我得到这个讨厌的错误:

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

有人知道如何解决这个错误吗?

1l5u6lss

1l5u6lss1#

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

在目标说明符后面加一个斜杠。参见说明和源代码。

fv2wmkja

fv2wmkja2#

本手册在以下三节中介绍了此方案:

我将展示一个可复制的示例,而不是从文档中复制粘贴所有内容(因为复制的一些片段确实不够;这是一个多方面的问题)-但是,请注意导入Map中的值,因为它们是通过阅读文档的所有三个链接部分得出的:
./import_map.json

{
  "imports": {
    "chalk": "npm:chalk@5.2.0",
    "node:util": "https://deno.land/std@0.170.0/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

使用定义的任务在终端中运行:

% 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

到目前为止,德诺的一切都很棒。
让我们检查一下相同的代码在Node.js中是否可以正常工作。需要添加以下文件以使用Node进行编译和运行,因为它不包括Deno的所有内置工具:
./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

// 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
  }
}

使用定义的npm脚本在终端中运行:

% 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

> so-74905332@0.1.0 dev
> tsc && node src/linked-queue.js

util: object
chalk: function

% echo $?
0

相同的模块源代码也适用于Node.js。

相关问题