You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js项目启动报错:connection非函数与dotenv模块缺失

Node.js项目启动报错解决方案

问题1:TypeError: connection is not a function

根据你提到的原因(解构引入connection,但db.js是默认导出函数),有两种修复方式:

  • 调整index.js的引入代码:如果db.js用的是module.exports = connection这类默认导出写法,把index.js里的const { connection } = require('./db.js')改成const connection = require('./db.js')
  • 或者调整db.js的导出方式:如果想保留解构引入的写法,把db.js里的默认导出改成命名导出,比如module.exports = { connection },这样index.js的解构代码就能正常运行

问题2:Error: Cannot find module 'dotenv'

按以下步骤修复:

  • 安装dotenv依赖:在项目根目录执行命令 npm install dotenv
  • 在项目入口文件(比如index.js)的最顶部添加代码:require('dotenv').config(),确保环境变量能被正确加载

内容的提问来源于stack exchange,提问作者sujith Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 18:12:35