buildpacks icon indicating copy to clipboard operation
buildpacks copied to clipboard

Cannot build typescript project

Open briandealwis opened this issue 4 years ago • 10 comments

Our npm buildpack runs npm ci to install the runtime dependencies only. But TypeScript projects seem to often install the typescript toolchain via devDependencies and then run tsc via a postinstall script, and so fail to install. Perhaps we should npm install followed by an npm prune.

For an example, try building the VSCode typescript-in-docker example.

Status: Image is up to date for gcr.io/buildpacks/gcp/run:latest
===> DETECTING
[detector] 2 of 4 buildpacks participating
[detector] google.nodejs.runtime 0.9.0
[detector] google.nodejs.npm     0.9.0
===> ANALYZING
===> RESTORING
===> BUILDING
[builder] === Node.js - Runtime ([email protected]) ===
[builder] Resolving Node.js version based on semver ""
[builder] Using resolved runtime version from package.json: 14.2.0
[builder] Installing Node.js v14.2.0
[builder] === Node.js - NPM ([email protected]) ===
[builder] Installing application dependencies.
[builder] ————————————————————————————————————————————————————————————————————————————————
[builder] Running "npm ci --quiet (NODE_ENV=production)"
[builder] 
[builder] > [email protected] postinstall /workspace
[builder] > tsc -p ./src
[builder] 
[builder] sh: 1: tsc: not found
[builder] npm ERR! code ELIFECYCLE
[builder] npm ERR! syscall spawn
[builder] npm ERR! file sh
[builder] npm ERR! errno ENOENT
[builder] npm ERR! [email protected] postinstall: `tsc -p ./src`
[builder] npm ERR! spawn ENOENT
[builder] npm ERR! 
[builder] npm ERR! Failed at the [email protected] postinstall script.
[builder] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[builder] 
[builder] npm ERR! A complete log of this run can be found in:
[builder] npm ERR!     /home/cnb/.npm/_logs/2020-05-07T19_08_30_604Z-debug.log
[builder] Done "npm ci --quiet (NODE_ENV=production)" (737.806ms)
[builder] Failure: sh: 1: tsc: not found
[builder] npm ERR! code ELIFECYCLE
[builder] npm ERR! syscall spawn
[builder] npm ERR! file sh
[builder] npm ERR! errno ENOENT
[builder] npm ERR! [email protected] postinstall: `tsc -p ./src`
[builder] npm ERR! spawn ENOENT
[builder] npm ERR! 
[builder] npm ERR! Failed at the [email protected] postinstall script.
[builder] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[builder] 
[builder] npm ERR! A complete log of this run can be found in:
[builder] npm ERR!     /home/cnb/.npm/_logs/2020-05-07T19_08_30_604Z-debug.log
[builder] ————————————————————————————————————————————————————————————————————————————————
[builder] Sorry your project couldn't be built.
[builder] Our documentation explains ways to configure Buildpacks to better recognise your project:
[builder]  -> https://github.com/GoogleCloudPlatform/buildpacks/blob/master/README.md
[builder] If you think you've found an issue, please report it:
[builder]  -> https://github.com/GoogleCloudPlatform/buildpacks/issues/new
[builder] ————————————————————————————————————————————————————————————————————————————————
[builder] ERROR: failed to build: exit status 1
ERROR: failed with status code: 7

briandealwis avatar May 07 '20 19:05 briandealwis

You can work around this by specifying NODE_ENV=development, which is what npm install does by default and the project seems to assume. The prepare script should install devDependencies and then remove them, but there seems to be a bug where it does not if NODE_ENV=production which should be fixed in npm 7.

lukasberger avatar May 07 '20 19:05 lukasberger

Have similar issue when using NestJS. I have info that image was build (btw huge image size compared to my Dockerfile) but when I'm trying to run it it's not starting.

Mistic92 avatar May 11 '20 09:05 Mistic92

Is that with NODE_ENV=development or without? Is there an error message printed on startup?

lukasberger avatar May 11 '20 16:05 lukasberger

(btw huge image size compared to my Dockerfile)

What base image do you use in your Dockerfile? The run image we use (gcr.io/buildpacks/gcp/run) includes few packages and is 111MB vs 941mb for the official node:14 image.

A missing required package could be the reason the app is failing to start.

lukasberger avatar May 11 '20 23:05 lukasberger

I'm using alpine and slim versions of node 12. On node 13 I had issue with grpc.

Mistic92 avatar May 12 '20 07:05 Mistic92

I was able to avoid setting NODE_ENV=development by defining the gcp-build script as documented here. I didn't trace back what actually checks for the gcp-build script to execute it but it may be the buildpack.

kwilcox avatar Sep 30 '20 17:09 kwilcox

gcp-build has its own buildpack (source), which is available in App Engine and Cloud Functions.

You can override NODE_ENV at build time using -e NODE_ENV=development if using pack directly or by creating .npmrc when deploying to GAE/GCF: https://github.com/GoogleCloudPlatform/buildpacks/issues/75#issuecomment-686656544.

lukasberger avatar Oct 01 '20 17:10 lukasberger

I have a new sample for this at: https://github.com/GoogleCloudPlatform/buildpack-samples/tree/master/sample-node-tsc

Because it is pretty common to use devDependencies to build the application, I think the buildpack should do something like the following by default:

  1. Install devDependencies
  2. The postInstall script then runs with devDependencies
  3. The deps only in devDependencies (not in dependencies) should then be removed (unless NODE_ENV=development)

jamesward avatar Apr 12 '21 15:04 jamesward

The google.nodejs.npm-gcp-build (https://github.com/GoogleCloudPlatform/buildpacks/blob/main/cmd/nodejs/npm_gcp_build/main.go) seems to be exactly what I'm looking for, but is it not available except for App Engine and Functions?

I thought I could use the buildpack by specifying it directly, but when I tried it, got an error.

$ pack build sample --builder gcr.io/buildpacks/builder --buildpack [email protected]

latest: Pulling from buildpacks/builder
Digest: sha256:1d7aedab5c2c440bcab0ab891316f1450e3b6fc9e92186a193d4b806f6e887ac
Status: Image is up to date for gcr.io/buildpacks/builder:latest
v1: Pulling from buildpacks/gcp/run
Digest: sha256:46d844b2b02f3af89936cbc4ed0fc350809c7a71dc73cf9557bb067c5968a5cd
Status: Image is up to date for gcr.io/buildpacks/gcp/run:v1
ERROR: failed to build: invalid buildpack string [email protected]

The prepare script should install devDependencies and then remove them, but there seems to be a bug where it does not if NODE_ENV=production which should be fixed in npm 7.

Unfortunately, prepare script does not seem to behave as expected in npm 7 or later.

tomoemon avatar Jul 07 '21 13:07 tomoemon

I fixed it by including a gcp-build script in my package.json file.

"scripts": {
    "start": "node ./build/server/index.js",
    "gcp-build": "tsc"
  },

BahaaZidan avatar Jun 17 '22 18:06 BahaaZidan

Typescript projects should be buildable now that we run npm run build by default https://cloud.google.com/docs/buildpacks/nodejs#executing_custom_build_steps_during_deployment

jama22 avatar Jun 28 '23 21:06 jama22