javascript - 无法获取 __dirname 值
问题描述
我__dirname
用来获取 GraphQL 模式的绝对路径:
const schema = loadSchemaSync(path.join(__dirname, './graphql/schemas/schema.graphql'), {
loaders: [new GraphQLFileLoader()]
});
我已更改模块以适应 ES6 模块标准,__dirname
现在未定义。
如何解析模式的路径?
解决方案
esm
+有一些问题__dirname
ES 模块和 CommonJS 的区别
没有 __filename 或 __dirname
这些 CommonJS 变量在 ES 模块中不可用。
__filename 和 __dirname 用例可以通过 import.meta.url 复制。
尝试通过此示例修复 https://nodejs.org/api/esm.html#esm_import_meta_url
loadSchemaSync(path.join(import.meta.url, './graphql/schemas/schema.graphql'), ...
推荐阅读
- python - Python 中的快速傅里叶变换性能
- javascript - Discord.js 机器人赠品命令:UnhandledPromiseRejectionWarning
- bash - 使用 `git submodule foreach` 运行包含 git 命令的脚本会导致 `not a git repository` 错误
- python - 在 AWS Glue 中将字符串转换为时间戳
- python-3.x - 如何在我的 Django 项目中使用过滤器搜索表
- sed - 如何将一个命令应用于另一个 sed 命令?
- c# - 如何使类工厂仅启用生成的对象的某些属性
- mantis - 螳螂自定义状态仅在设置时更改新状态?
- wpf - Grid WPF 中的用户控件和对象列表
- swift - 为什么我收到“MultiSelectSegmentedControl 的位置不明确”?