我试图使用node模块,结果出现了报错

看了 [ 使用标准模块中的 node 模块](deno如何使用npm包?) 我也想尝试一下,结果运行后报错

源码:

import { createRequire } from "https://deno.land/std/node/module.ts";

const require = createRequire(import.meta.url);
// Loads native module polyfill.
const path = require("path");
console.log("test",path);

执行命令 deno run --allow-read .\app.ts

错误信息:

Compile file:///D:/code/deno/app.ts
error: TS2339 [ERROR]: Property 'dir' does not exist on type 'typeof Deno'.
  return Deno.dir("home");
              ~~~
    at https://deno.land/std/node/os.ts:139:15

TS2339 [ERROR]: Property 'hostname' does not exist on type 'typeof Deno'.
  return Deno.hostname();
              ~~~~~~~~
    at https://deno.land/std/node/os.ts:144:15

TS2339 [ERROR]: Property 'loadavg' does not exist on type 'typeof Deno'.
  return Deno.loadavg();
              ~~~~~~~
    at https://deno.land/std/node/os.ts:152:15

TS2339 [ERROR]: Property 'osRelease' does not exist on type 'typeof Deno'.
  return Deno.osRelease();
              ~~~~~~~~~
    at https://deno.land/std/node/os.ts:166:15

TS2339 [ERROR]: Property 'dir' does not exist on type 'typeof Deno'.
  return Deno.dir("tmp");
              ~~~
    at https://deno.land/std/node/os.ts:185:15

TS2339 [ERROR]: Property 'Signal' does not exist on type 'typeof Deno'.
  signals: Deno.Signal,
                ~~~~~~
    at https://deno.land/std/node/os.ts:219:17

Found 6 errors.

我不知道该如何解决这个问题

试试加上 --unstable

deno run --allow-read --unstable
2 个赞

感谢,是这个原因。