Assess your understanding of handling environment variables and configuration files in modern build processes using Parcel. This quiz explores best practices, variable scoping, naming conventions, and secure configuration management for optimal project setup.
Which environment variable naming prefix is required for a value to be accessible in client-side code using Parcel?
Explanation: The correct prefix is PUBLIC_, which tells the build process to expose the variable to client-side JavaScript. NODE_ is typically used for server-side environments and does not make variables available in client bundles. APP_ and ENV_ look similar to correct answers but are not recognized by default to enable variable exposure. Using the incorrect prefix may result in the variable not being injected at build time.
Why should sensitive data such as API keys be stored in a separate configuration file and not in source code when working with Parcel?
Explanation: Storing sensitive data in a separate, non-versioned config file ensures secrets are not accidentally exposed to anyone who receives the client bundle. Configuration files are not universally faster to load, and syntax errors relate to code validity rather than data storage. Build tools can read environment variables from many sources, not only configuration files, so that option is incorrect.
How can you safely reference an environment variable in a Parcel-built client-side JavaScript file?
Explanation: Referencing an environment variable as process.env.PUBLIC_MY_KEY allows Parcel to replace it at build time with its value, provided it has the proper PUBLIC_ prefix. Importing config.json directly is not recommended unless it is designed for client-side use and free from secrets. Hardcoding values defeats the purpose of environment variables. process.env.SECRET_KEY lacks the required PUBLIC_ prefix, so it will not be injected.
If a required environment variable is missing from your .env file in Parcel, what is a recommended way to provide a fallback value?
Explanation: Using the logical OR operator (||) in your code allows you to specify a default if the variable is missing, for example: process.env.PUBLIC_API_URL || 'https://default.url'. Adding comments or renaming the variable does not provide a fallback at runtime. Leaving variables undefined introduces errors rather than enforcing strictness.
What is an effective method to handle different environment configurations (such as development and production) in a Parcel project?
Explanation: Creating environment-specific files like .env.development and .env.production and ensuring the build process uses the correct one for each environment is a reliable technique for configuration management. Repeating variable declarations is inefficient and error-prone. Relying only on global configuration with fixed values removes flexibility. Disabling environment variables in production hinders proper configuration and is not recommended.