我正在尝试使用Google Cloud语音文本API。
我使用的是示例Google代码,当我创建客户端对象时,出现此错误。
{
"errno":-2,
"syscall":"open",
"code":"ENOENT",
"path":"protos.json",
"stack":"Error: ENOENT: no such file or directory, open 'protos.json'\n at Object.openSync (fs.js:440:3)\n at Object.readFileSync (fs.js:342:35)\n at fetch (transcript-server-js/node_modules/protobufjs/src/root.js:160:34)\n at Root.load (/transcript-server-js/node_modules/protobufjs/src/root.js:194:13)\n at Root.loadSync (/transcript-server-js/node_modules/protobufjs/src/root.js:235:17)\n at Object.loadSync (/transcript-server-js/node_modules/@grpc/proto-loader/build/src/index.js:221:27)\n at GrpcClient.loadFromProto /transcript-server-js/node_modules/google-gax/src/grpc.ts:165:40)\n at GrpcClient.loadProto (/transcript-server-js/node_modules/google-gax/src/grpc.ts:199:17)\n at new SpeechClient /transcript-server-js/lib/webpack:/src/v1/speech_client.ts:135:28)\n at createText$ (/transcript-server-js/lib/webpack:/src/transcriptGenerator.js:50:18)"
}
这是代码
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
const results = await storage.getBuckets();
const speech = require('@google-cloud/speech');
const client = new speech.SpeechClient();
google cloud storage api可以使用。
有人可以帮我吗?
谢谢
答案 0 :(得分:4)
我遇到了@google-cloud/firestore
。 @google-cloud/firestore
和@google-cloud/speech
都使用相同的机制来加载protos.json
,因此我的解决方案在此处应具有针对性。
发生这种情况是因为webpack正在将@google-cloud/firestore
软件包构建到我的捆绑软件中。 @google-cloud/firestore
包使用__dirname
查找protos.json
。由于@google-cloud/firestore
代码位于我的捆绑软件中,因此__dirname
变量被设置为捆绑软件的目录,而不是包含node_modules/@google-cloud/firestore/
的{{1}}子目录。
在webpack配置中对此进行设置,以告诉webpack设置protos.json
的值:
__dirname
https://webpack.js.org/configuration/node/
更新您的webpack配置,以将 node: {
__dirname: true,
}
从捆绑中排除。
执行此操作的一种方法是使用@google-cloud/speech
包从webpack-node-externals
目录中排除所有依赖项:
node_modules
答案 1 :(得分:0)
非常感谢加布里埃尔交易。 我在 firestore 包中遇到了和你一样的问题。我从你的解释中明白为什么会发生这种情况。不幸的是,这些修复对我没有帮助。所以我不得不采取替代。我将 protos.json 文件复制到它在我的 dist 文件夹中搜索的路径。
module.exports = {
.
.
.
plugins: [
new CopyWebpackPlugin([
{ from: "external_files/protos.json", to: "dist/node_modules/google-gax/protos" }
])
]
}