Next.js中MoralisProvider报错:TypeError: Right-hand side of 'instanceof' is not callable
问题
在Next.js项目中使用react-moralis的MoralisProvider组件,代码如下:
import { MoralisProvider } from 'react-moralis' function MyApp({ Component, pageProps }) { return ( <MoralisProvider serverUrl={process.env.NEXT_PUBLIC_MORALIS_SERVER} appId={process.env.NEXT_PUBLIC_MORALIS_APP_ID} > <Component {...pageProps} /> </MoralisProvider> ) } export default MyApp
运行时触发未处理的运行时错误:
Unhandled Runtime Error TypeError: Right-hand side of 'instanceof' is not callable
原因分析
这个错误的核心原因是react-moralis与moralis核心库版本不兼容。新版本react-moralis对moralis的版本有明确依赖要求,若两者版本不匹配,会导致内部类型校验时,instanceof右侧的预期构造函数无法被正确调用——可能是模块未正确导出或加载。
此外,依赖安装时的缓存问题也可能引发该异常,导致库的部分模块损坏或未完整加载。
解决方法
- 对齐依赖版本:查看
react-moralis的package.json文件,确认它依赖的moralis版本范围,安装对应版本的moralis。例如,若react-moralis要求moralis@^2.0.0,执行:npm install moralis@^2.0.0 react-moralis@latest # Yarn用户执行: yarn add moralis@^2.0.0 react-moralis@latest - 清理缓存并重装依赖:若版本已对齐仍报错,大概率是缓存问题,按以下步骤操作:
- 删除
node_modules文件夹:rm -rf node_modules - 删除锁文件:
rm package-lock.json # npm用户 # 或 rm yarn.lock # Yarn用户 - 重新安装所有依赖:
npm install # 或 yarn install
- 删除
- 验证环境变量配置:虽然该错误与环境变量无直接关联,但需确保
NEXT_PUBLIC_MORALIS_SERVER和NEXT_PUBLIC_MORALIS_APP_ID在Next.js项目中配置正确,避免后续衍生问题。
内容的提问来源于stack exchange,提问作者Subhamay Nath
相关产品推荐
相关产品推荐

