Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables not included after running adapter-node build using node build/index.js #7987

Closed
xpluscal opened this issue Dec 7, 2022 · 7 comments · Fixed by #8020
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@xpluscal
Copy link

xpluscal commented Dec 7, 2022

Describe the bug

When creating a project setting env vars and accessing them via $env/dynamic/private.

The env vars appear fine when running npm run dev and running npm run preview after build, but are not accessible when running the app using node build/index.js.

I provided a minimal repo for reproduction.

Reproduction

https://github.com/xpluscal/svelte-env-test

Logs

No response

System Info

System:
    OS: macOS 12.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
    Memory: 974.33 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.8.0 - ~/.nvm/versions/node/v18.8.0/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v18.8.0/bin/yarn
    npm: 8.18.0 - ~/.nvm/versions/node/v18.8.0/bin/npm
  Browsers:
    Brave Browser: 104.1.42.97
    Chrome: 108.0.5359.94
    Firefox: 92.0.1
    Safari: 16.0
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.90 
    @sveltejs/adapter-node: ^1.0.0-next.102 => 1.0.0-next.102 
    @sveltejs/kit: next => 1.0.0-next.572 
    svelte: ^3.53.1 => 3.54.0 
    vite: ^3.2.4 => 3.2.5

Severity

blocking all usage of SvelteKit

Additional Information

No response

@Conduitry
Copy link
Member

This works for me with your repro. Are you sure the environment variables are actually set in the application? If you run

FOO=bar
node build/index.js

the environment variable won't actually be passed down to the child process. You either need to use export FOO=bar or you need to run FOO=bar node build/index.js.

Also, if you are setting env vars with a .env file, those aren't loaded by the production app - you need to have the variables actually set when running it.

@xpluscal
Copy link
Author

xpluscal commented Dec 8, 2022

Thank you for the swift feedback.
I did run a test run with FOO=bar node build/index.js, which worked.

Also, if you are setting env vars with a .env file, those aren't loaded by the production app - you need to have the variables actually set when running it.

Could you explain that?

In our production app we have a .env file set and the app doesn't seem to recognize it.
Do we need to separately run dotenv config or does svelte do that?

@xpluscal
Copy link
Author

xpluscal commented Dec 8, 2022

We previously used dotenv, but have ditched that in favor of the new svelte env logic.
Is there a way to get them into the app just using the .env file?

@Rich-Harris
Copy link
Member

dotenv is still the way to go if you need to load the contents of an .env file in production. (SvelteKit does this in dev and preview, but in production you're expected to provide the environment.)

Instead of running node build, you can do node -r dotenv/config build. Opened #8020 to document this.

@xpluscal
Copy link
Author

xpluscal commented Dec 9, 2022

dotenv is still the way to go if you need to load the contents of an .env file in production. (SvelteKit does this in dev and preview, but in production you're expected to provide the environment.)

Instead of running node build, you can do node -r dotenv/config build. Opened #8020 to document this.

Thank you that was super helpful and exactly the information I needed. Much appreciated!

@xpluscal
Copy link
Author

The interesting thing is that while the dotenv will work for all other envs, for ORIGIN it will not.
I’m not entirely clear how adapter-node pulls in that var, but the only solution was to prefix it in the npm start command. Not pretty since this has to be changed depending on the environment.

@Conduitry
Copy link
Member

We'd need a specific reproduction for that. It doesn't make a lot of sense to me, because ORIGIN is grabbed right off process.env the way any other variable would be. https://github.com/sveltejs/kit/blob/master/packages/adapter-node/src/env.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants