javascript - 库使用 __dirname 导致模块中的错误
问题描述
所以我有一个type
设置为module
in的打字稿应用程序package.json
。
到目前为止,一切都很好,但是现在在我导入了谷歌语音 api 之后,在尝试运行应用程序时一切都中断了。
> node build --prod
file:///opt/jap/server/build/index.js:132416
var googleProtoFilesDir = path.join(__dirname, "..", "..", "protos");
^
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/opt/jap/server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
我想有点道理,但我无法更改应用程序的类型,而且.js
文件不是我可以控制的,因为它是依赖项。
我试过在没有运气的情况下添加,我已经添加"types": ["node"]
为依赖项,也没有任何成功。compilerOptions
tsconfig.json
"@types/node": "^16.9.1"
那么有什么明显的解决方案吗?
"esModuleInterop": true
还有:我确实有tsconfig.json
解决方案
推荐阅读
- postgresql - PostgreSQL 慢命令
- postgresql - 使用 KafkaConnect JDBC 源发布记录时出错:STRUCT 的类型无效:类 org.apache.avro.generic.GenericData$Record
- plot - Stata中带有xline的直方图
- python - 如何让我的不和谐机器人删除一个 nsfw 词,而不是另一个包含 nsfw 词的 sfw 词?(discord.py)
- arrays - 如何将重复的二维数组减少为一个
- python - Numpy通过降序对二维数组进行排序并从每行中取前N
- spring - 自 Spring Cloud Stream 3.1 版本以来已弃用 EnableBinding、Output、Input
- c# - 单击xamarin中的按钮时如何为整个帧设置动画?
- css - 使用@font-face 时字体边缘变细
- asp.net - Docker 错误:注册层失败:处理 tar 文件时出错(退出状态 1):“...msader15.dll.mui:没有这样的文件或目录”