NodeJS | Variável de Ambiente | #23

NodeJS | Variável de Ambiente | #23

Introduction to JavaScript Course

Overview of the Course Journey

  • The speaker welcomes participants to a new JavaScript course, highlighting the progress made so far in their learning journey.
  • Acknowledges the extensive content covered up to this point, emphasizing the importance of good practices as they move forward.

Transition to Best Practices

  • Introduces the upcoming module focused on best practices, stressing the need for organized and professional code structure.
  • Begins discussing environment variables, explaining their significance in coding and application development.

Understanding Environment Variables

Definition and Importance

  • Defines environment variables as system-wide variables accessible by all programs running on an operating system.
  • Contrasts local program variables with environment variables, noting that local variables exist only during program execution while environment variables persist across sessions.

Practical Application

  • Discusses how any program can read environment variables created at the OS level, making them crucial for flexible application deployment.
  • Mentions differences in creating and accessing environment variables across different operating systems (e.g., Windows vs. Mac).

Using Environment Variables in Node.js

Why Use Environment Variables?

  • Explains that certain information should not be hardcoded into applications; instead, it should be stored as environment variables for flexibility when deploying on different servers.
  • Provides an example regarding server ports: using an environment variable allows easy changes without modifying source code.

Accessing Environment Variables

  • Demonstrates how to access an environment variable in Node.js using process.env syntax.
  • Illustrates how to create an environment variable through terminal commands specific to each operating system (e.g., export for Mac and set for Windows).

Creating and Displaying Environment Variables

Command Syntax

  • Shows command syntax for setting an environment variable (export VAR_NAME=value on Mac).
  • Highlights potential confusion between commands used on different systems when displaying or altering these values.

Example Execution

Understanding Environment Variables in Terminal

Setting Up Environment Variables

  • To display an environment variable, use the syntax %VARIABLE_NAME%. On Mac, there may be issues with this command.
  • For testing on Windows, utilize set along with the percentage signs around the variable name. On Mac, use export.

Scope of Environment Variables

  • An environment variable created in a terminal session is only available within that specific terminal instance.
  • If you set a variable (e.g., PORT=5002) and run your code, it will read from that terminal's environment variables.

Issues with Nodemon and Process Management

  • The issue arises because Nodemon runs in a separate process and does not recognize the environment variables set in the original terminal.
  • To ensure Nodemon recognizes the variable, you can export it directly before starting your application.

Correcting Port Configuration

  • Use export PORT=5002 && npm start to execute commands sequentially; this ensures both commands are recognized.
  • If hardcoding port numbers in your code (like 5001), it will always default to that unless dynamically linked to an environment variable.

Testing Variable Persistence Across Terminals

  • Changing an environment variable requires restarting your application for changes to take effect; simply changing values won't update them live.
  • When setting a new port (e.g., PORT=5000), ensure you restart your server for it to reflect correctly.

Global vs Local Environment Variables

  • Each terminal maintains its own set of variables; creating global variables is possible but requires additional configuration.
  • You can create multiple environment variables easily through terminal commands without modifying code files directly.

Managing Multiple Environment Variables

  • As applications grow, managing numerous environment variables becomes complex; consider using packages designed for handling these configurations efficiently.

How to Manage Environment Variables in Node.js

Introduction to Environment Variables

  • Managing environment variables can be cumbersome, especially when needing to define them repeatedly. A solution is provided by the dotenv package.
  • To use this package, it must first be imported using import 'dotenv/config', which allows for configuration management.

Setting Up the .env File

  • The .env file is created at the root of the project, allowing all environment variables to be stored in one place. Node.js automatically reads this file upon startup.
  • If there are issues starting the application (e.g., due to existing terminal environment variables), restarting the terminal and running npm start again resolves these problems.

Benefits of Using .env Files

  • The .env file simplifies managing multiple environment variables, making it easier to launch applications without manually setting each variable in the terminal.
  • This approach enhances security since sensitive information can be kept out of version control systems like GitHub by ignoring the .env file.

Security Considerations

  • It’s crucial not to upload .env files containing sensitive data to public repositories. Instead, they should be included in a .gitignore file.
  • During deployment, developers need to create their own .env files on production servers for proper application functionality while ensuring data integrity and security.

Conclusion