Ricerca nel sito web

node.js - Impossibile trovare il modulo 'node:url' durante l'esecuzione di TypeScript da WebStorm


Ho scritto questo piccolo esempio di 'hello world' in TypeScript

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');

quando eseguo questo da WebStorm ottengo il seguente errore

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

Qualcuno ha idea di come risolvere questo? Ho già provato npm install node:url and url

Ho isolato l'errore in questa riga

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

Risposta n. 1

Il problema potrebbe essere legato alla versione di Node.

L'axios-cookiejar-support richiede una versione specifica di Node ("node": ">=14.18.0 <15.0.0 || >=16.0.0").

Controlla node --version e package-lock.json.

Esempio :

    "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"
          }
        },

Risposta n. 2

assicurati che l'array types nel tuo file tsconfig.json contenga "node"

{
  "compilerOptions": {
    "types": [
      // ... your other types
      "node"
    ],
    // ... your other settings
  },
}

Articoli correlati