Functions bundling ignores my Environment Variable

sitename: ‘neegzar’
domain: neegzar.com

Hey,
I have a problem with node-fluent-ffmpeg.
The solution is to add this environment variable:

        'process.env.FLUENTFFMPEG_COV': false

(This is not the code I used, this is the solution provided here) - to add the environment varaible.

And so that’s what I did:

I’ve tried adding the environment EVERYWHERE but it still ignores this variable.

  • I’ve tried to add as to netlify settings → Environment Variable

Image: Pasteboard - Uploaded Image

  • I tried in netlify.toml file like this:
# Settings in the [build] context are global and are applied to 
# all contexts unless otherwise overridden by more specific contexts.
# [build]

[build.environment]
  FLUENTFFMPEG_COV = ""

# Production context: all deploys from the Production branch 
# set in your site’s Branches settings in the UI will inherit 
# these settings. You can define environment variables 
# here but we recommend using the Netlify UI for sensitive
# values to keep them out of your source repository. 
[context.production]
  environment = { FLUENTFFMPEG_COV = "false" }

But everything I’ve tried still ignores this environment variable.

Build error:

────────────────────────────────────────────────────────────────
3:06:26 PM:   2. Functions bundling                                         
3:06:26 PM: ────────────────────────────────────────────────────────────────
3:06:26 PM: ​
3:06:26 PM: Packaging Functions from .netlify/functions-internal directory:
3:06:26 PM:  - render.js
3:06:26 PM: ​
3:06:26 PM: Packaging Functions from netlify/functions directory:
3:06:26 PM:  - create-gallery-image.ts
3:06:26 PM:  - is-production.ts
3:06:26 PM:  - meta.js
3:06:26 PM:  - record-mandala.ts
3:06:26 PM:  - take-screenshot.js
3:06:26 PM:  - test.cjs
3:06:26 PM: ​
3:06:28 PM: ✘ [ERROR] Could not resolve "./lib-cov/fluent-ffmpeg"
3:06:28 PM:     node_modules/fluent-ffmpeg/index.js:1:56:
3:06:28 PM:       1 │ ...FMPEG_COV ? require('./lib-cov/fluent-ffmpeg') : require('./lib/...        ╵                        ~~~~~~~~~~~~~~~~~~~~~~~~~
3:06:29 PM: ​
3:06:29 PM: ────────────────────────────────────────────────────────────────
3:06:29 PM:   Dependencies installation error                               
3:06:29 PM: ────────────────────────────────────────────────────────────────
3:06:29 PM: ​
3:06:29 PM:   Error message
3:06:29 PM:   A Netlify Function failed to require one of its dependencies.
3:06:29 PM:   Please make sure it is present in the site's top-level "package.json".​
3:06:29 PM:   Build failed with 1 error:
3:06:29 PM:   node_modules/fluent-ffmpeg/index.js:1:56: ERROR: Could not resolve "./lib-cov/fluent-ffmpeg"
3:06:29 PM: ​
3:06:29 PM:   Resolved config
3:06:29 PM:   build:
3:06:29 PM:     command: npm run build
3:06:29 PM:     commandOrigin: ui
3:06:29 PM:     environment:
3:06:29 PM:       - FLUENTFFMPEG_COV
3:06:29 PM:       - VITE_AWS_ACCESS_KEY_ID
3:06:29 PM:       - VITE_AWS_BUCKET_NAME
3:06:29 PM:       - VITE_AWS_SECRET_ACCESS_KEY
3:06:29 PM:       - VITE_GOOGLE_SPREADSHEETS_PRIVATE_NEEGZAR_API_KEY
3:06:29 PM:       - VITE_MONGO_DB_URI
3:06:29 PM:     publish: /opt/build/repo/build
3:06:29 PM:     publishOrigin: ui
3:06:29 PM:   functionsDirectory: /opt/build/repo/netlify/functions
3:06:29 PM:   headers:
3:06:32 PM:     - for: /_app/immutable/*      values:        cache-control: public, immutable, max-age=31536000  redirects:    - from: '*'      status: 200      to: /.netlify/functions/renderCaching artifacts
3:06:32 PM: Started saving node modules
3:06:32 PM: Finished saving node modules
3:06:32 PM: Started saving build plugins
3:06:32 PM: Finished saving build plugins
3:06:32 PM: Started saving pip cache
3:06:32 PM: Finished saving pip cache
3:06:32 PM: Started saving emacs cask dependencies
3:06:32 PM: Finished saving emacs cask dependencies
3:06:32 PM: Started saving maven dependencies
3:06:32 PM: Finished saving maven dependencies
3:06:32 PM: Started saving boot dependencies
3:06:32 PM: Finished saving boot dependencies
3:06:32 PM: Started saving rust rustup cache
3:06:32 PM: Finished saving rust rustup cache
3:06:32 PM: Started saving go dependencies
3:06:32 PM: Finished saving go dependencies
3:06:32 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:06:33 PM: Failing build: Failed to build site
3:06:33 PM: Finished processing build request in 45.765355418s

The environment variable isn’t a boolean

According to the post you’ve attached, the environment variable needs to be a string.
Which it is.
So why is it still ignoring it?

The post also says

environment variables are always returned as strings.

If environment variables are always returned as strings, that means

"FALSE" ? "one" : "two" // returns one

Which is bad.

In other words, we need a way to return “two” as a string.

Is “” counts as a string?
If so,

"" ? 'one' : 'two' // should return two

and so I changed the variables to

[build.environment]
  FLUENTFFMPEG_COV = ""
[context.production]
  environment = { FLUENTFFMPEG_COV = "" }

But it still returns the first argument (lib-cov) instead of (lib).

...FMPEG_COV ? require('./lib-cov/fluent-ffmpeg') : require('./lib/...

In other words, Netlify ignores “” (empty) environment varaibles, but in my case we still need to print out “”… am I corrent? what’s the solution for this?

Are you comparing the variable as a String with ===?

No, @elron is not… but that’s because the comparison is happening in the dependency, not @elron’s code.

  • The environment variables must be Strings
  • The code expects a falsey value
  • To provide a false value with a string it would need to be empty
  • @elron tried to provide an empty string and indicates it didn’t work

Checking the dependency on GitHub it looks like they changed how the folder is resolved in 2019:

You might want to look at upgrading the dependency if you can.

1 Like

Thanks for the suggestion.
Just wanted to update that I had no choice but to move my function to AWS Lambda.
It worked there with FFMPEG layer.