Cannot find module 'node:url' when executing typescript from webstorm

Viewed 1050

I have written this small typescript hello world example

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

when I run this from webstorm i get the following error

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

anyone have idea on how to resolve this? I already tried npm install node:url and url

i have isolated the error to this line

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

The issue maybe is related to the node version.

The axios-cookiejar-support requires a specific node version ("node": ">=14.18.0 <15.0.0 || >=16.0.0").

Check node --version and package-lock.json.

Sample:

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

make sure the types array in your tsconfig.json file contains "node"

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

You might need to install a later version of node.js.

I was running 14.17.6 and after installing 16.17.0 with nvm then I was able to run the project.

If you have nvm installed you can install a specific version of node e.g.

nvm install 16.17.0
Related