Skip to content

v4.26.0+ loads .env automatically #917

Closed

Description

@sapphi-red

It seems .env is loaded automatically when importing browserslist.

# .env
TEST=1
import 'browserslist'
console.log('process.env.TEST', process.env.TEST) // "process.env.TEST 1" is output

I don't expect browserslist to have this feature.
This happens since 4.26.0.

Activity

  1. ai commented on Dec 5, 2025

    @ai
    Member

    @tonypconway could it be from baseline dependency?

  2. tonypconway commented on Dec 5, 2025

    @tonypconway
    Contributor

    baseline-browser-mapping is using the process.loadEnvFile() feature of Node.js where it's available to check for the BASELINE_BROWSER_MAPPING_IGNORE_OLD_DATA and BROWSERSLIST_IGNORE_OLD_DATA env variables, but if this is causing unexpected behaviour I can patch it out and update our docs.

    @sapphi-red what's your expectation here? That you put stuff in a .env files but tools will only read it if you explicitly tell them to?

  3. ai commented on Dec 5, 2025

    @ai
    Member

    Why not just checking process.env?

    Yes, unfortunately, it is unexpected behaviour for most of people.

  4. tonypconway commented on Dec 5, 2025

    @tonypconway
    Contributor

    Fair enough, I'll patch it out. process.env doesn't include variables set in .env files unless you use the process.loadEnvFile() function first, but that support is relatively recent (Node 20+) even though using .env files is common practice in a lot of frameworks, but I guess they're doing manual file reads rather than using process.env. I'll patch it out and update the docs to point out that you need to proactively load the .env file if you don't want to use CLI params.

  5. sapphi-red commented on Dec 5, 2025

    @sapphi-red
    Author

    I don't expect a package to load env files automatically when simply importing the package. I'm not sure about process.loadEnvFile's behavior, but if it overrides the existing env var, it will be confusing. Because that would mean that the env var will be overridden when importing your package with a dynamic import.

    In our case, Vite loads env files and the way it parses env files are incompatible with Node.js's one (which was introduced after we (and the other tools) have been doing...). To make things worse, Vite does not override the env vars when loading the env vars so that you can override the env var. This means if browserslists (/ baseline-browser-mapping) loads the env files, an incorrect (in terms of Vite's env var feature due to different parsing) value would be set.

  6. tonypconway commented on Dec 5, 2025

    @tonypconway
    Contributor

    Thanks for the detail! I'll mirror what browserslist does i.e. just checking process.env and not using the process.loadEnvFile() as it seems to be causing difficulty for people on Vite.

    @ai I've noticed that the BROWSERSLIST_IGNORE_OLD_DATA var isn't documented anywhere - is that intentional to discourage people from ignoring old data warnings? The way baseline-browser-mapping is set up now, people will see these warnings extremely rarely, if at all, but if I'm giving a recommendation for how to use them it would be good to have it be consistent with your guidance.

  7. ai commented on Dec 5, 2025

    @ai
    Member

    I've noticed that the BROWSERSLIST_IGNORE_OLD_DATA var isn't documented anywhere - is that intentional to discourage people from ignoring old data warnings

    Exactly 😉

    I am disabling your warning anyway (with argument) so you can avoid checking BROWSERSLIST_IGNORE_OLD_DATA

  8. tonypconway commented on Dec 5, 2025

    @tonypconway
    Contributor

    Lol, good to know 👀 I'm going to leave the check in there in case you decide at some point in the future to remove the suppressWarning for people targeting newly or a very far in the future widelyAvailableOnDate - no pressure to do that now, but saves me a job down the line.

  9. tonypconway commented on Dec 5, 2025

    @tonypconway
    Contributor

    @sapphi-red baseline-browser-mapping@2.9.3 was just released. Try updating to that version or @latest and let me know if you're still seeing the unexpected behaviour.

  10. sapphi-red commented on Dec 6, 2025

    @sapphi-red
    Author

    Thanks, I confirmed that it's fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions