gpt4 book ai didi

TypeScript - `redis` module is undefined when running output file(类型脚本-运行输出文件时未定义`redis`模块)

转载 作者:bug小助手 更新时间:2023-10-26 19:58:16 27 4
gpt4 key购买 nike



I switched a Discord bot project from JavaScript to TypeScript.

我把一个不一致的机器人项目从脚本改成了打字脚本。


But then running the compiled code (using tcs), the redis module is undefined. The compilation process success, other modules (discord.js, dotenv, etc) are imported and used just fine, requiring the redis module work but output a undefined.

但是运行编译后的代码(使用TCS)时,redis模块是未定义的。编译过程成功,其他模块(discord.js、dotenv等)被导入并使用得很好,需要redis模块工作,但输出一个未定义的。


My tsconfig.json (based on @tsconfig/node16):

我的tsconfig. json(基于@tsconfig/node 16):


{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"rootDir": "./src/",
"outDir": "./dist/",
"importHelpers": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"removeComments": true,
"typeRoots": [
"node_modules/@types"
],
"sourceMap": false,
"baseUrl": "./"
},
"files": [
"src/index.ts"
],
"include": [
"./**/*.ts"
],
"exclude": [
"node_modules",
"dist"
]
}

I have tried many things including removing typeRoots and exclude.

我尝试了很多方法,包括删除typeRoots和排除。


My package.json:

我的Package.json:


{
"name": "discord-bot",
"version": "0.1.0",
"description": "A bot to manage roles and such on Discord",
"main": "index.js",
"scripts": {
"start": "tsc && node dist/index.js",
"build": "tsc",
},
"dependencies": {
"@tsconfig/node16": "^16.1.0",
"@types/node": "^16.9.0",
"discord.js": "^14.11.0",
"dotenv": "^16.3.1",
"redis": "^4.6.7",
"typescript": "^5.1.6"
}
}

All dependencies have been updated to latest and support NodeJS 16.

所有依赖项都已更新到最新版本,并支持NodeJS 16。


The start of my code:

我的代码的开头:


import Discord from "discord.js"
import redis from "redis"
import http from "http"
import "dotenv/config"
import * as process from "process"

// Initialise Discord Client
const discordClient: Discord.Client<true> = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildIntegrations
]
})

// Create the database client
const redisClient = redis.createClient()

I am getting a TypeError: Cannot read properties of undefined (reading 'createClient') at runtime at the last line, even if the compilation work fine and my IDE autocompleting the library functions properly.

我收到一个TypeError:无法在运行时在最后一行读取未定义的属性(读取‘createClient’),即使编译工作正常并且我的IDE自动完成库功能也是如此。


(please note that I am still a beginner in TypeScript in Node contexts, coming from React TypeScript with frameworks)

(请注意,我在节点环境中的TypeScrip方面仍然是初学者,来自Reaction TypeScrip With Frameworks)


更多回答

That looks correct to me but I normally see folks just importing createClient directly using import { createClient } from 'redis'. Does that change anything?

这在我看来是正确的,但我通常会看到人们使用从‘redis’导入的{createClient}直接导入createClient。这会改变什么吗?

That's really strange, but using this syntax make the script work fine. I didn't change the syntax used for importing the module when migrating to TypeScript, so it was working in pure JS. I have also no idea why the compilation would work then and the IDE autocomplete too. I just checked the node-redis source code and it seems to not have a default export?? github.com/redis/node-redis/blob/master/index.ts Its also annoying IMO to not beeing able to prefix the createClient function with redis since its a pretty common name and create confusion inside the file.

这真的很奇怪,但是使用这种语法可以让脚本工作得很好。在迁移到TypeScrip时,我没有更改用于导入模块的语法,因此它在纯JS中工作。我也不知道为什么编译会工作,IDE自动完成也会工作。我刚刚检查了node-redis的源代码,它似乎没有默认的导出??不能使用redis作为createClient函数的前缀,这也让github.com/redis/node-redis/blob/master/index.ts很恼火,因为它是一个非常常见的名称,并在文件中造成混淆。

@AFCMS if you want to prefix with redis you could try importing like this: import * as redis from "redis"

@AFCMS如果您想以redis作为前缀,可以尝试如下导入:从“redis”导入*as redis

优秀答案推荐

base on their redis documentation this is how I see on their snippet

根据他们的redis文档,这就是我在他们的代码片段上看到的


import { createClient } from 'redis';

const client = createClient();

client.on('error', err => console.log('Redis Client Error', err));

await client.connect();

await client.set('key', 'value');
const value = await client.get('key');
await client.disconnect();

upon checking on their type definition from node_modules/redis/dist/index.d.ts there are no default export that I found, so try to use the named export or create all alias for them import * as redis from 'redis'

在检查NODE_MODULES/redis/dist/index.d.ts中的类型定义时,我没有找到默认的导出,因此尝试使用命名的导出或为它们创建所有别名从‘redis’导入*as redis


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com