![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
env-schema
Advanced tools
The env-schema npm package is used to validate environment variables against a schema. It ensures that the environment variables are present and conform to the expected types and constraints, providing a robust way to manage configuration in Node.js applications.
Schema Validation
This feature allows you to define a schema for your environment variables and validate them. The code sample demonstrates how to define a schema with required properties and default values, and then validate the environment variables against this schema.
const envSchema = require('env-schema');
const schema = {
type: 'object',
required: ['PORT', 'HOST'],
properties: {
PORT: {
type: 'number',
default: 3000
},
HOST: {
type: 'string',
default: 'localhost'
}
}
};
const config = envSchema({ schema: schema });
console.log(config);
Default Values
This feature allows you to specify default values for environment variables. If the environment variable is not set, the default value will be used. The code sample shows how to set default values for the PORT and HOST environment variables.
const envSchema = require('env-schema');
const schema = {
type: 'object',
properties: {
PORT: {
type: 'number',
default: 3000
},
HOST: {
type: 'string',
default: 'localhost'
}
}
};
const config = envSchema({ schema: schema });
console.log(config);
Type Coercion
This feature allows you to coerce environment variables to the specified types. The code sample demonstrates how to coerce the PORT environment variable to a number and the DEBUG environment variable to a boolean.
const envSchema = require('env-schema');
const schema = {
type: 'object',
properties: {
PORT: {
type: 'number'
},
DEBUG: {
type: 'boolean'
}
}
};
process.env.PORT = '3000';
process.env.DEBUG = 'true';
const config = envSchema({ schema: schema });
console.log(config);
Joi is a powerful schema description language and data validator for JavaScript. It can be used to validate environment variables, but it is more general-purpose and can validate any JavaScript object. Compared to env-schema, Joi offers more flexibility and a richer set of validation features, but it requires more setup for environment variable validation.
Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. While it does not provide schema validation, it is often used in conjunction with validation libraries like Joi or env-schema to manage environment variables in Node.js applications.
Convict is a configuration management tool for Node.js that allows you to define a schema for your configuration, including environment variables. It provides validation, default values, and type coercion similar to env-schema, but it also includes features for managing configuration files and command-line arguments.
Utility to check environment variables using JSON schema, Ajv and dotenv.
npm install --save env-schema
const envSchema = require('env-schema')
const schema = {
type: 'object',
required: [ 'PORT' ],
properties: {
PORT: {
type: 'string',
default: 3000
}
}
}
const config = envSchema({
schema: schema,
data: data // optional, default: process.env
dotenv: true // load .env if it's there, default: false
})
console.log(config)
// output: { PORT: 3000 }
It is possible to also use fluent-schema:
const envSchema = require('env-schema')
const S = require('fluent-schema')
const config = envSchema({
schema: S.object().prop('port', S.string().default('3000').required()),
data: data // optional, default: process.env
dotenv: true // load .env if it's there, default: false
})
console.log(config)
// output: { PORT: 3000 }
NB: internally this plugin force to not have additional properties,
so the additionalProperties
flag is forced to be false
This library supports the following Ajv custom keywords:
separator
Type: string
Applies to type: string
When present, the value provided will be split based by this value.
Example:
const envSchema = require('env-schema')
const schema = {
type: 'object',
required: [ 'ALLOWED_HOSTS' ],
properties: {
ALLOWED_HOSTS: {
type: 'string',
separator: ','
}
}
}
const data = {
ALLOWED_HOSTS: '127.0.0.1,0.0.0.0'
}
const config = envSchema({
schema: schema,
data: data, // optional, default: process.env
dotenv: true // load .env if it's there, default: false
})
// config.data => ['127.0.0.1', '0.0.0.0']
Kindly sponsored by Mia Platform and NearForm.
MIT
FAQs
Validate your env variables using Ajv and dotenv
The npm package env-schema receives a total of 2,848 weekly downloads. As such, env-schema popularity was classified as popular.
We found that env-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.