Search code examples
node.jsnext.jsvercelgoogle-speech-api

Can I use a node module such as require('npm-package') with nextjs vercel, google-cloud-speech


I'd like to use google cloud speech with my nextjs vercel applicaiton, but having trouble importing the module`

const speech = require('google-cloud/speech');

I tried configuring the webpack5 file, but not getting anything logged for the speech

//next.config.js
module.exports = withBundleAnalyzer({  \
  webpack5: true,
  webpack: (config) => {
    config.resolve.fallback = { 'google-cloud/speech': false };

    return config;
  },
})

any help would be appreciated

api/route file

import { getAuth } from "firebase/auth";
import dynamic from "next/dynamic";
const speech = require('google-cloud/speech');



export async function POST(req: NextRequest) {
  console.log(speech, 'lx')
  const body = await req.json();

Solution

  • If this is the package you're attempting to use, then you should refer to it with @google-cloud/speech in your import. Note the @. You should double check you've installed it correctly by running npm i @google-cloud/speech.

    Additionally, it looks like you're using ES syntax (see es vs commonjs imports) in your route file, so you should actually import the module like this:

    import speech from '@google-cloud/speech'
    

    Then, I would recommend reading up on its documentation for how to correctly use it here. Best of luck!