@ google-cloud / speech-错误:ENOENT:没有这样的文件或目录,请打开'protos.json

时间:2019-12-17 16:07:20

标签: node.js google-cloud-platform speech-to-text

我正在尝试使用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可以使用。

有人可以帮我吗?

谢谢

2 个答案:

答案 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}}子目录。

可能的解决方法#1

在webpack配置中对此进行设置,以告诉webpack设置protos.json的值:

__dirname

https://webpack.js.org/configuration/node/

可能的解决方案2

更新您的webpack配置,以将 node: { __dirname: true, } 从捆绑中排除。

执行此操作的一种方法是使用@google-cloud/speech包从webpack-node-externals目录中排除所有依赖项:

node_modules

答案 1 :(得分:0)

非常感谢加布里埃尔交易。 我在 firestore 包中遇到了和你一样的问题。我从你的解释中明白为什么会发生这种情况。不幸的是,这些修复对我没有帮助。所以我不得不采取替代。我将 protos.json 文件复制到它在我的 dist 文件夹中搜索的路径。

  1. protos.json 从 node_modules 复制到一个文件夹(我将其命名为 external_files
  2. webpack.config.js 中,将 protos.json 文件从 external_files 目录复制到它正在搜索的路径(在我的例子中,它是在 node_modules/google- gax/protos)。使用插件 CopyWebpackPlugin 来完成如下所示的工作。
module.exports = {
.
.
.
    plugins: [
        new CopyWebpackPlugin([
            { from: "external_files/protos.json", to: "dist/node_modules/google-gax/protos" }
        ])
    ]
}