nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

Environment variables without dotenv dependency in nodejs v20, not working using Docker. #4258

Closed Jaaiirr closed 1 month ago

Jaaiirr commented 9 months ago

Details

I have a problem, I'll explain: Currently, I am doing an upgrade in a microservice, moving from version 18 to version 20.8.0, with the aim of no longer using the dotenv dependency. My problem is in the Dockerfile. When creating the container, it does not recognize the environment variables. I have searched for help in forums but I can't find relevant information on this topic. Is anyone going through a similar situation that can guide me?

Node.js version

{ node: '20.8.0', acorn: '8.10.0', ada: '2.6.0', ares: '1.19.1', base64: '0.5.0', brotli: '1.0.9', cjs_module_lexer: '1.2.2', cldr: '43.1', icu: '73.2', llhttp: '8.1.1', modules: '115', napi: '9', nghttp2: '1.56.0', nghttp3: '0.7.0', ngtcp2: '0.8.1', openssl: '3.0.10+quic', simdutf: '3.2.17', tz: '2023c', undici: '5.25.2', unicode: '15.0', uv: '1.46.0', uvwasi: '0.0.18', v8: '11.3.244.8-node.16', zlib: '1.2.13.1-motley' }

Example code

FROM --platform=linux/amd64 node:20.8.0-alpine3.18

WORKDIR /app

COPY . .

COPY .env .env

ENV APP_ENV .env

CMD ["sh", "-c", "sleep 1; npm run migrate:latest; sleep 1; node --env-file .env index.js"]

Operating system

Linux eddy 6.2.0-33-generic #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 7 10:33:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Scope

Code

Module and version

Not applicable.

preveen-stack commented 9 months ago

CMD ["sh", "-c", "sleep 1; npm run migrate:latest; sleep 1; node --env-file .env index.js"]

I do not see an --env-file option for node, Which node version are you using ?

Can you check out https://nodejs.dev/en/learn/how-to-read-environment-variables-from-nodejs/

kshitijsaksena commented 8 months ago

Hi @Jaaiirr In the CMD operation the command should be node --env-file=.env. Also ENV APP_ENV .env does not make sense as APP_ENV cannot be equal to a file. It takes key:value pair.

Reference: https://nodejs.org/en/blog/release/v20.6.0 https://docs.docker.com/engine/reference/builder/#env

github-actions[bot] commented 2 months ago

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] commented 1 month ago

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.