Search code examples
node.jsgoogle-cloud-platformspeech-to-text

@google-cloud/speech - Error: ENOENT: no such file or directory, open 'protos.json


I'm trying to use the google cloud speech to text api.

I'm using the sample google code and when i create the client object i got this error.

{ 
   "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)"
}

this is the code

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 works.

can someone help me?

thanks


Solution

  • I ran into this with @google-cloud/firestore. Both @google-cloud/firestore and @google-cloud/speech use the same mechanism to to load protos.json, so my solution should be relevant here.

    This happened to me because webpack was building the @google-cloud/firestore package into my bundle. The @google-cloud/firestore package uses __dirname to find protos.json. Since the @google-cloud/firestore code was in my bundle, the __dirname variable was set to my bundle's directory instead of to the node_modules/@google-cloud/firestore/ subdirectory that contains protos.json.

    Possible fix #1

    Set this in your webpack config to tell webpack to set the value of __dirname:

        node: {
          __dirname: true,
        }
    

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

    Possible fix #2

    Update your webpack config to exclude @google-cloud/speech from your bundle.

    One way to do this is to use the webpack-node-externals package to exclude all dependencies from the node_modules directory:

    var nodeExternals = require('webpack-node-externals')
    ...
    module.exports = {
      ...
      externals: [nodeExternals()],
      target: 'node',
      ...
    };