PostGraphile集成upload-field插件时出现模块缺失与运行报错
PostGraphile 上传字段插件报错排查与修复
模块加载失败问题
- 已执行的插件安装命令为
npm i graphile-contrib/postgraphile-plugin-upload-field - 首次启动时的完整命令为:
postgraphile -c 'postgres://postgres:mysecretpass@192.168.38.4:5432/gimapp' --cors -p 5001 --enhance-graphiql --host "0.0.0.0" -s messages,basic_auth,user --append-plugins @graphile/postgis,@graphile-contrib/pg-simplify-inflector,postgraphile-plugin-connection-filter,@graphile-contrib/postgraphile-plugin-upload-field --watch --no-ignore-indexes --jwt-token-identifier basic_auth.jwt_token --jwt-secret mysecretpass - 启动后抛出完整错误堆栈为:
Failed to load plugin '@graphile-contrib/postgraphile-plugin-upload-field' /usr/lib/node_modules/postgraphile/build/postgraphile/cli.js:268 throw e; ^ Error: Cannot find module '@graphile-contrib/postgraphile-plugin-upload-field' Require stack: - /usr/lib/node_modules/postgraphile/build/postgraphile/cli.js - /usr/lib/node_modules/postgraphile/cli.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15) at Function.Module._load (node:internal/modules/cjs/loader:769:27) at Module.require (node:internal/modules/cjs/loader:997:19) at require (node:internal/modules/cjs/helpers:92:18) at /usr/lib/node_modules/postgraphile/build/postgraphile/cli.js:263:20 at Array.map (<anonymous>) at loadPlugins (/usr/lib/node_modules/postgraphile/build/postgraphile/cli.js:255:18) at Object.<anonymous> (/usr/lib/node_modules/postgraphile/build/postgraphile/cli.js:323:23) at Module._compile (node:internal/modules/cjs/loader:1108:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10) { code: 'MODULE_NOT_FOUND', requireStack: [ '/usr/lib/node_modules/postgraphile/build/postgraphile/cli.js', '/usr/lib/node_modules/postgraphile/cli.js' ] }
- 问题本质:通过GitHub仓库地址安装的插件,实际包名定义为不带npm作用域的
postgraphile-plugin-upload-field,引用带@graphile-contrib作用域前缀的名称时,模块解析逻辑无法匹配到对应包文件。
插件名调整后的运行时错误
- 为解决模块找不到的问题,移除插件名前的
@graphile-contrib前缀,调整后的启动命令为:postgraphile -c 'postgres://postgres:mysecretpass@192.168.38.4:5432/gimapp' --cors -p 5001 --enhance-graphiql --host "0.0.0.0" -s messages,basic_auth,user --append-plugins @graphile/postgis,@graphile-contrib/pg-simplify-inflector,postgraphile-plugin-connection-filter,postgraphile-plugin-upload-field --watch --no-ignore-indexes --jwt-token-identifier basic_auth.jwt_token --jwt-secret mysecretpass - 调整命令后可正常启动服务,但连续抛出两次可恢复运行时错误,完整报错内容为:
PostGraphile v4.10.0 server listening on port 5001 🚀 Recoverable error occurred; use envvar 'DEBUG="graphile-build:warn"' for full error (详见官方调试文档说明) > TypeError: Cannot read property 'filter' of undefined… Recoverable error occurred; use envvar 'DEBUG="graphile-build:warn"' for full error (详见官方调试文档说明)
- 问题本质:插件加载顺序错误。当前
--append-plugins列表中将上传字段插件放在最后加载,该插件初始化时依赖前置插件先完成schema上下文的初始化工作,顺序颠倒会导致访问未初始化的上下文对象时报错。
修复操作步骤
- 替换安装方式:使用npm官方源的正式发布版安装插件,避免直接拉取GitHub仓库源码导致的包路径不匹配问题,全局安装命令为:
npm i -g postgraphile-plugin-upload-field,和全局安装的PostGraphile保持同一路径,解决cli模块解析问题。 - 调整插件加载顺序:将上传字段插件从列表末尾移到靠前位置,修正后的插件列表顺序为:
@graphile/postgis,postgraphile-plugin-upload-field,@graphile-contrib/pg-simplify-inflector,postgraphile-plugin-connection-filter - 带调试信息的完整启动命令参考:
DEBUG="graphile-build:warn" postgraphile -c 'postgres://postgres:mysecretpass@192.168.38.4:5432/gimapp' --cors -p 5001 --enhance-graphiql --host "0.0.0.0" -s messages,basic_auth,user --append-plugins @graphile/postgis,postgraphile-plugin-upload-field,@graphile-contrib/pg-simplify-inflector,postgraphile-plugin-connection-filter --watch --no-ignore-indexes --jwt-token-identifier basic_auth.jwt_token --jwt-secret mysecretpass - 注意事项:直接通过GitHub仓库地址安装的插件,除非手动配置全局路径软链接,否则PostGraphile的cli执行时会因为模块解析范围的限制,无法找到项目目录下安装的本地包,不推荐这种安装方式。
内容的提问来源于stack exchange,提问作者Mustafa Ülker
相关产品推荐
相关产品推荐

