在使用WebStorm执行TypeScript时,出现“找不到模块'node:url'”的错误。

8
我已经编写了这个小的 TypeScript Hello World 实例。
import axios from 'axios';
import { wrapper } from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';

const jar = new CookieJar();
const client = wrapper(axios.create({ jar }));

client.get('https://example.com');

当我从WebStorm运行时,会出现以下错误。
/usr/bin/node /usr/local/lib/node_modules/ts-node/dist/bin.js /home/nayana/WebstormProjects/hello-world/hello.ts
Error: Cannot find module 'node:url'

有人知道如何解决这个问题吗? 我已经尝试了npm install node:url and url

我已将错误隔离到这一行。

const client = wrapper(axios.create({ jar }));

2
你是否已经安装了 @types/node - Meowster
有关 node: 协议的更多信息:https://2ality.com/2021/12/node-protocol-imports.html - Meowster
两个建议都没有起作用。 - DevZer0
4个回答

8

问题可能与node版本有关。

axios-cookiejar-support需要特定的node版本("node": ">=14.18.0 <15.0.0 || >=16.0.0")。

请检查node --versionpackage-lock.json

示例:

    "node_modules/axios-cookiejar-support": {
          "version": "4.0.3",
          "resolved": "https://registry.npmjs.org/axios-cookiejar-support/-/axios-cookiejar-support-4.0.3.tgz",
          "integrity": "sha512-fMQc0mPR1CikWZEwVC6Av+sD4cJuV2eo06HFA+DfhY54uRcO43ILGxaq7YAMTiM0V0SdJCV4NhE1bOsQYlfSkg==",
          "dependencies": {
            "http-cookie-agent": "^4.0.2"
          },
          "engines": {
            "node": ">=14.18.0 <15.0.0 || >=16.0.0"
          },
          "peerDependencies": {
            "axios": ">=0.20.0",
            "tough-cookie": ">=4.0.0"
          }
        },

4
你可能需要安装更新版本的Node.js。
我原本使用的是14.17.6版本,之后通过nvm安装了16.17.0版本,然后就能运行该项目了。
如果你已经安装了nvm,你可以安装指定版本的Node,例如:
nvm install 16.17.0

2
确保您的tsconfig.json文件中的types数组包含“node”。
{
  "compilerOptions": {
    "types": [
      // ... your other types
      "node"
    ],
    // ... your other settings
  },
}

Cannot find type definition file for 'node'. - undefined
@Codesmith 运行 npm install --save-dev @types/node - undefined

0

如果您没有安装 TypeScript,您需要做的唯一事情就是在 vite.config.js 文件中更改导入行,如下所示:

import { fileURLToPath, URL } from 'node:url'

To:

import { fileURLToPath, URL } from 'url'

1
同样的错误,但是关于 'url' 模块。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接