nodejs-datastore icon indicating copy to clipboard operation
nodejs-datastore copied to clipboard

Cannot open file `app_engine_key.proto` after build with esbuild

Open giangm9 opened this issue 3 years ago • 9 comments

I was making a Google AppEngine project. I use the google app datastore as the following code

import { Datastore } from "@google-cloud/datastore";

then bundle using https://esbuild.github.io/ After that, an error occurred

  + node --enable-source-maps server.js dev
Error: ENOENT: no such file or directory, open '/home/gl/protos/app_engine_key.proto'
    at Object.openSync (fs.js:476:3)
    at Object.readFileSync (fs.js:377:35)

Environment details

  • OS: Ubuntu 20.0 (wsl)
  • Node.js version: 14
  • npm version: 6.14.10
  • @google-cloud/datastore version: 6.3.1

Steps to reproduce

  1. esbuild src/server/index.tsx --minify --bundle --jsx-factory=h --log-level=error --color=true --sourcemap --platform=node --outfile=server.js
  2. node --enable-source-maps server.js dev

giangm9 avatar Jan 17 '21 21:01 giangm9

Hi @giangm9. Thank you for reporting, and sorry for the delay. This module, and others that we use in our client libraries, require .proto files. These are included with the module in the published npm package. From the module's source files, the proto files are located with Node's built-in directory traversing mechanisms, such as the magic variable __dirname. That becomes an issue with esbuild, because when it bundles all of the source code files into one, it removes the directory hierarchy that our modules expect to exist.

Unfortunately, I'm not very familiar with esbuild-- do you know if there's a way to work around this type of problem?

stephenplusplus avatar Mar 29 '21 17:03 stephenplusplus

@bcoe do you know if we are able to support this type of use?

stephenplusplus avatar May 10 '21 15:05 stephenplusplus

@alexander-fenster @summer-ji-eng is this a use case that's supported now, by virtue of us shipping JSON versions of protos files (am I imagining that we made this change?).

bcoe avatar Jun 08 '21 16:06 bcoe

I'm bumping into this exact issue. I'm using webpack + typescript. I copied /protos/app_engine_key.proto separately to the project root folder, but no luck. I imagine my webpack configuration lacks something but I'm not too familiar with how it works. Any advice?

agektmr avatar Jul 08 '21 02:07 agektmr

I just realized I don't need to bundle server side code. Please ignore my question.

agektmr avatar Jul 08 '21 12:07 agektmr

@bcoe @alexander-fenster ?

crwilcox avatar Jul 23 '21 22:07 crwilcox

@crwilcox @giangm9 while it can be possible to get things working, with some workarounds, we do not currently support bundlers in our gRPC libraries.

There's a feature tracking thread here it's something we very much would like to support.

bcoe avatar Jul 28 '21 19:07 bcoe

In the meantime, you might have better luck with the REST client.

bcoe avatar Jul 28 '21 19:07 bcoe

Hi all, wanted to share the solution I'm using, which is to exclude the @google-cloud/datastore package from bundling altogether: https://esbuild.github.io/api/#external

Then, you just need to include the @google-cloud/datastore dependency in your package.json that you deploy to the cloud function (as it's obviously not part of the bundle anymore)

This way you don't have to disable bundling for your other packages, so it's a nice middle ground.

Cheers!

shane-lamb avatar Nov 09 '21 06:11 shane-lamb

Same issue when using NextJS (v13). Anyone any idea how to resolve? Looks like now I just cannot use Datastore with NextJS.

joosttk avatar Mar 16 '23 15:03 joosttk

hey folks - I've faced such issue in Next.js 13.4 The solution is to update the next.config.js:

const nextConfig = {
  experimental: {
    // don't bundle the package
    serverComponentsExternalPackages: ['@google-cloud/datastore'],
    // enable alpha server actions
    serverActions: true,
  },  
}

And explicitly specify that a page or a component are server-side by adding 'use server'

cc @joosttk

b4lk0n avatar May 10 '23 15:05 b4lk0n

@b4lk0n works for me!

anestesya avatar Sep 12 '23 12:09 anestesya

Closing this out as the workaround seems to be effective. Re: web support, we are considering this across our Node.js portfolio of libraries and are unlikely to do it with just one library. Please follow this issue for the overall project: https://github.com/googleapis/google-cloud-node/issues/2933

meredithslota avatar Nov 08 '23 03:11 meredithslota