最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Uncaught SyntaxError: The requested module does not provide an export named 'Redis' in ioredis - Stack

programmeradmin1浏览0评论

I am using ioredis in my Node.js + Express project with ES modules ("type": "module" in package.json). When I try to import Redis like this:

import {Redis} from "ioredis";
import dotenv from "dotenv";

dotenv.config();

const redis = new Redis(process.env.UPSTASH_REDIS_URI);

// ✅ Correct Export (Named + Default)
export { redis };
export default redis;

I get the error:

Uncaught SyntaxError: The requested module  
  '/@fs/D:/Main/Web_Development/Projects/E-Commerce-
    Store/node_modules/ioredis/built/index.js?v=0293eaae'
      does not provide an export named 'Redis' (at redis.js:1:9)

I tried changing the import statement to:

import Redis from "ioredis";

But this resulted in another error:

Uncaught SyntaxError: The requested module  
 '/@fs/D:/Main/Web_Development/Projects/E-Commerce- 
   Store/node_modules/ioredis/built/index.js?v=0293eaae'  
     does not provide an export named 'default' (at redis.js:1:8)
  • I also checked node_modules/ioredis/built/index.js to see if Redis is exported, but couldn't find a named export.
  • I expected the module to import Redis correctly without errors.
  • i am using the latest ioredis, version which is 5.5.0

here's my vite.config.js:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      "/api": {
        target: "http://localhost:5000",
       },
    },
  },

  build: {
    rollupOptions: {
      // Exclude ioredis from the final build
      external: ["ioredis"], 
    },
  },

  optimizeDeps: {
    // Prevent Vite from pre-bundling ioredis
    exclude: ["ioredis"], 
  },

});

here's my package.json file:

{
  "name": "e-commerce-store",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon backend/server.js",
    "start": "node backend/server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "type": "module",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cloudinary": "^2.5.1",
    "cookie-parser": "^1.4.7",
    "cors": "^2.8.5",
    "dotenv": "^16.4.7",
    "express": "^4.21.2",
    "ioredis": "^5.5.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.9.5",
    "razorpay": "^2.9.5",
    "redis": "^4.7.0",
    "safe-buffer": "^5.2.1",
    "stripe": "^17.5.0"
  },
  "devDependencies": {
    "nodemon": "^3.1.9"
  }
}



I am using ioredis in my Node.js + Express project with ES modules ("type": "module" in package.json). When I try to import Redis like this:

import {Redis} from "ioredis";
import dotenv from "dotenv";

dotenv.config();

const redis = new Redis(process.env.UPSTASH_REDIS_URI);

// ✅ Correct Export (Named + Default)
export { redis };
export default redis;

I get the error:

Uncaught SyntaxError: The requested module  
  '/@fs/D:/Main/Web_Development/Projects/E-Commerce-
    Store/node_modules/ioredis/built/index.js?v=0293eaae'
      does not provide an export named 'Redis' (at redis.js:1:9)

I tried changing the import statement to:

import Redis from "ioredis";

But this resulted in another error:

Uncaught SyntaxError: The requested module  
 '/@fs/D:/Main/Web_Development/Projects/E-Commerce- 
   Store/node_modules/ioredis/built/index.js?v=0293eaae'  
     does not provide an export named 'default' (at redis.js:1:8)
  • I also checked node_modules/ioredis/built/index.js to see if Redis is exported, but couldn't find a named export.
  • I expected the module to import Redis correctly without errors.
  • i am using the latest ioredis, version which is 5.5.0

here's my vite.config.js:

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  server: {
    proxy: {
      "/api": {
        target: "http://localhost:5000",
       },
    },
  },

  build: {
    rollupOptions: {
      // Exclude ioredis from the final build
      external: ["ioredis"], 
    },
  },

  optimizeDeps: {
    // Prevent Vite from pre-bundling ioredis
    exclude: ["ioredis"], 
  },

});

here's my package.json file:

{
  "name": "e-commerce-store",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon backend/server.js",
    "start": "node backend/server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "type": "module",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cloudinary": "^2.5.1",
    "cookie-parser": "^1.4.7",
    "cors": "^2.8.5",
    "dotenv": "^16.4.7",
    "express": "^4.21.2",
    "ioredis": "^5.5.0",
    "jsonwebtoken": "^9.0.2",
    "mongoose": "^8.9.5",
    "razorpay": "^2.9.5",
    "redis": "^4.7.0",
    "safe-buffer": "^5.2.1",
    "stripe": "^17.5.0"
  },
  "devDependencies": {
    "nodemon": "^3.1.9"
  }
}



Share Improve this question edited yesterday avifen 1,0037 silver badges18 bronze badges asked Feb 16 at 16:42 Rakesh NagarkarRakesh Nagarkar 12 bronze badges New contributor Rakesh Nagarkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 10
  • I tried, it didn't gave me any errors, may be because of ioredis version ? – bogdanoff Commented Feb 16 at 16:56
  • 2 "i am using latest 'ioredis' version which is 10.8.2" but the latest version is only 5.5.0 on npm. Post your package.json file. – jQueeny Commented Feb 16 at 18:03
  • Are you using typescript by any chance? – avifen Commented Feb 16 at 19:08
  • 1 Based on the console logs you posted you might be using Vite (@fs is typically something I see in Vite logs). If you are could you please post your Vite config as well as any other configurations (e.g. TypeScript)? – M. Damian Mulligan Commented Feb 16 at 19:09
  • if this is the case you should use npm install --save-dev @types/node and import Redis and not { Redis } – avifen Commented Feb 16 at 19:13
 |  Show 5 more comments

1 Answer 1

Reset to default 1

You have in your dependency both node-redis and io-redis. So you should choose. For node-redis, the one called redis use:

import { createClient } from 'redis';

const client = createClient();

await client.connect();

For io-redis:

const Redis = require("ioredis");
const redis = new Redis();

But the best is always valkey-glide :P

import { GlideClient } from "@valkey/valkey-glide";
const addresses = [
    {
        host: "localhost",
        port: 6379,
    },
];
const client = await GlideClient.createClient({
    addresses: addresses,
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论