paketo-buildpacks / npm-install

A Cloud Native Buildpack for npm
Apache License 2.0
10 stars 17 forks source link

Always overriding NODE_ENV with development #691

Open MilosMoravac opened 6 months ago

MilosMoravac commented 6 months ago

Expected Behavior

When NODE_ENV is specified it should override development and not the other way around.

Current Behavior

Whatever is set during build environment install process it will be overridden to development

Possible Solution

Steps to Reproduce

  1. Pull samples/web-servers/react-frontend-sample
  2. pack build --clear-cache paketo-demo-app --builder paketobuildpacks/builder-jammy-base
  3. Logs for buildpack for NPM install 1.4.0 show following:
    Configuring build environment NODE_ENV -> "development" PATH -> "$PATH:/layers/paketo-buildpacks_npm-install/build-modules/node_modules/.bin"

    Motivations

    First of all I am a devops so this isn't really my cup of tea. So this is an issue my team has run into while using vite where the front builds and its throwing them errors. Whatever we do in nginx.toml it still uses the NODE_ENV=development. In the buildpack source code I found an override that always overwrites the variable you set with development I could be wrong but don't have many ideas left. I've tried locally to append env variable to 'pack build ' using --env but it does nothing.

This is the line of code I am suspecting is the issue. layer.BuildEnv.Override("NODE_ENV", "development") located here https://github.com/paketo-buildpacks/npm-install/blob/7b388f01af53beba6500526d3787d119610a50e5/build.go#L159

Lancelot0105 commented 3 months ago

Hey @MilosMoravac , did you find any workaround for this issue ?

c0d1ngm0nk3y commented 2 months ago

The original reason for this seems that the dev_dependencies should be available for the other buildpacks during build (see comment). Overwriting the user input feels wrong.

c0d1ngm0nk3y commented 2 months ago

After digging a bit deeper into the code, I don't think the problem is https://github.com/paketo-buildpacks/npm-install/blob/ae1768877f1382105967b078ba99be4d90218009/build.go#L159

but rather https://github.com/paketo-buildpacks/npm-install/blob/ae1768877f1382105967b078ba99be4d90218009/build.go#L131

which will call process.Run always with launch=false and then https://github.com/paketo-buildpacks/npm-install/blob/ae1768877f1382105967b078ba99be4d90218009/install_build_process.go#L49 will be added.

The "problem" is that this is by intention. It will always install the dev_dependencies for the build_modules and use npm prune for the launch_modules which will remove the dev_dependencies I think.

MilosMoravac commented 2 months ago

Hey @MilosMoravac , did you find any workaround for this issue ?

So in our pipeline when we build the image using packeto we set --env NODE_ENV="production" and that fixed it for us

Lancelot0105 commented 2 months ago

@MilosMoravac Even I tried explicitly setting the NODE_ENV="production" but still during the build time it is building with NODE_ENV as "development". Could you please share the command and logs which made you get the production NODE_ENV ? We are stuck with this blocker for a really long time and would really like a solution/workaround for this.

jwhitcraft commented 1 month ago

Yeah this is biting us as well. We've had to restort to doing this in our package.json files:

{
  "scripts": {
    "build": "NODE_ENV=production vite build",
  }
}
MilosMoravac commented 1 month ago

@MilosMoravac Even I tried explicitly setting the NODE_ENV="production" but still during the build time it is building with NODE_ENV as "development". Could you please share the command and logs which made you get the production NODE_ENV ? We are stuck with this blocker for a really long time and would really like a solution/workaround for this.

Sorry for late reply, we added --env NODE_ENV="production" to docker run buildpacksio/pack and it worked after that image was built.