Chucky Tasi
Chucky Tasi

Reputation: 11

redis : net.connect is not a Function

I need to add Redis to nodejs But it cannot be executed smoothly

Using the redis version, same code, but getting this error:

my code:

import { createClient } from 'redis'

export const client = createClient({
    socket: {
    host: "localhost",
    port: 6379
   },
  });

await client.connect();
console.log("client.isOpen: " + client.isOpen)

error:

socket.js:199 Uncaught (in promise) TypeError: net.connect is not a function
at RedisSocket._RedisSocket_createNetSocket (socket.js:199:17)
at eval (socket.js:175:346)
at new Promise (<anonymous>)
at RedisSocket._RedisSocket_createSocket (socket.js:171:10)
at RedisSocket._RedisSocket_connect (socket.js:148:148)
at RedisSocket.connect (socket.js:55:92)
at Commander.connect (index.js:196:66)
at eval (index.js:10:1)
at __webpack_require__.a (app.js:814:13)
at eval (index.js:1:21)

my package.json:

  "dependencies": {
  "-": "^0.0.1",
  "axios": "^1.6.3",
  "connect-redis": "^7.1.0",
  "core-js": "^3.8.3",
  "crypto-browserify": "^3.12.0",
  "dayjs": "^1.11.10",
  "for": "^0.0.1-dev.0",
  "install": "^0.13.0",
  "net": "^1.0.2",
  "node-polyfill-webpack-plugin": "^3.0.0",
  "node-redis": "^0.1.7",
  "redis": "^4.6.12",
  "stream-browserify": "^3.0.0",
  "tls": "^0.0.1",
  "url": "^0.11.3",
  "util": "^0.12.5",
  "vue": "^3.2.13",
  "vue-router": "^4.2.5"
 },

plead for help

use redis 3.1.2 but net.is-ip is not a Function

Upvotes: 1

Views: 418

Answers (1)

Krzysztof Krzeszewski
Krzysztof Krzeszewski

Reputation: 6749

Node redis relies on a core net library from nodejs.

Here is documentation

By installing an npm library "net": "^1.0.2" you are replacing the underlying core library with another one. Not sure why do you even install this library, it's clearly outdated, last published 10 years ago.

In general just remove this dependency from your package.json, rerun the npm install and it should fix your issues with redis.

Could you post the code using net library so we can address it instead? If it is necessary for your app, perhaps you should try using some dependency aliases. So that it does not have the same reserved core library name.

"dependencies": {
  "custom-name": "npm:net@^1.0.2",
}

It seems to me like you didn't really understand how npm dependencies really work. As most of your dependencies are unnecessary.

  • - library has no code, just an empty module, you probably installed it by accident
  • connect-redis requires express-session to be installed for it to work, so it's probably unused as well
  • for library has no code either, when installed you only download a package.json pointing to non existent index file
  • while install library has some contents, I assume you installed it by accident as well
  • node-polyfill-webpack-plugin is a webpack plugin, not sure how you plan on using it without webpack
  • node-redis is an old version of redis library, you shouldn't be installing both, especially that you probably only use redis anyway
  • both tls and net obfuscate the core nodejs libraries
  • you have both node specific and browser specific (like vue) libraries in one module, really you should create some division between front and back end

Upvotes: 0

Related Questions