buildpacks icon indicating copy to clipboard operation
buildpacks copied to clipboard

npm prune step does not remove devDependencies

Open steverandy opened this issue 1 year ago • 2 comments

I tried to deploy a basic node.js app that has devDependencies. It has a build step, so I included the gcp-build script in package.json file.

When I checked the build log, the npm prune step got executed, but it appears that it did not remove any of the devDependencies. I've verified this by comparing the build image size of a build that has devDependencies vs no devDependencies. Both have identical image size.

I've also compared running the npm prune in my localhost with NODE_ENV=production npm prune and the log output is different (about half no of packages reported in my localhost).

steverandy avatar Aug 02 '22 05:08 steverandy

Thanks for reporting this @steverandy. Could you tell me more about how you were building the app when the when the dev dependencies were not pruned? Were you deploying to App Engine or Google Cloud Functions? My first thought is that the NODE_ENV is not being set correctly, but I will need to understand more about how to repro this in order to chase it down.

matthewrobertson avatar Aug 18 '22 18:08 matthewrobertson

Deploying to Cloud Run. I will try to deploy a demo app and share the log with you.

If user does not set any NODE_ENV, is it correct to assume that buildpacks will prune the devDependencies (if any)?

steverandy avatar Aug 19 '22 06:08 steverandy

Any news on this one?

I have the same issue using gcloud app deploy app.yaml deploying to App Engine. No matter if I set env variable NODE_ENV: production or not, when gcp-build command (compiling TypeScript to JavaScript) has been executed, it is followed by npm prune which installs both dependencies and devDependencies (Cloud Build logs). And according to this doc:

"After executing your custom build step, App Engine removes and regenerates the node_modules folder by only installing the production dependencies declared in the dependencies field of your package.json file.".

GeorgiJefferski avatar Nov 17 '22 08:11 GeorgiJefferski

According to the npm docs, it seems we are doing the right thing:

If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies

Here is where the buildpacks are running npm prune. Interestingly the shouldPrune function in that file emits logging if it will skip this step. Are you folks seeing any logging about "Retaining devDependencies")?

matthewrobertson avatar Mar 03 '23 17:03 matthewrobertson

@matthewrobertson There is only Step #2 - "build": Running "npm prune" log, no "Retaining devDependencies because..." log. I am not a Go engineer, but shouldn't gcp.WithEnv("NODE_ENV="+nodejs.EnvProduction) be placed here (or even {"npm", "prune", "--production"/"--omit=dev"} because shouldPrune already checked for production environment)?

GeorgiJefferski avatar Mar 08 '23 07:03 GeorgiJefferski

Should be fixed now. 4a5e4e0

kennethye1 avatar Mar 10 '23 16:03 kennethye1