Consider running "scripts.build" by default for Node.js
It is very common for Node.js apps to have a "build" script, expected to be run at build time.
Example with a Next.js app:
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "17.0.2",
"react-dom": "17.0.2"
}
}
Today GCP Buildpacks do not run this build script.
I think I remember that if gcp-build script is present then it is run. I know that's what App Engine does and I can see traces of this in the code, however, it didn't seem to work when I tried.
Because it seems to be a widely adopted convension to use a build script in package.json, GCP Buildpacks could consider running it (alongside running gcp-build).
Today I tried to run my Next.js app using buildpack and also ran into this exact issue.
If I could avoid running npm build I'd try, but as far as I can tell Next requires this step.
$ docker run my-app
> [email protected] start /workspace
> next start
ready - started server on 0.0.0.0:8080, url: http://localhost:8080
warn - You have enabled experimental feature(s).
warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use them at your own risk.
Error: Could not find a production build in the '/workspace/.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
at NextNodeServer.getBuildId (/workspace/node_modules/next/dist/server/next-server.js:139:23)
at new Server (/workspace/node_modules/next/dist/server/base-server.js:90:29)
at new NextNodeServer (/workspace/node_modules/next/dist/server/next-server.js:86:9)
at NextServer.createServer (/workspace/node_modules/next/dist/server/next.js:109:16)
at async /workspace/node_modules/next/dist/server/next.js:121:31
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `next start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/cnb/.npm/_logs/2022-04-26T20_08_48_898Z-debug.log
I'm curious why Node.js "custom build steps" are not supported in the first place. Do you know?
Default behavior of Node.js buildpacks now runs build by default. https://cloud.google.com/docs/buildpacks/nodejs#executing_custom_build_steps_during_deployment