<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yuvraj Raghuvanshi</title>
    <description>The latest articles on DEV Community by Yuvraj Raghuvanshi (@yuvraj_raghuvanshi_1279d6).</description>
    <link>https://dev.to/yuvraj_raghuvanshi_1279d6</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3010601%2Ffa8e76fd-2288-42d4-903c-701e90cfb31f.jpg</url>
      <title>DEV Community: Yuvraj Raghuvanshi</title>
      <link>https://dev.to/yuvraj_raghuvanshi_1279d6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yuvraj_raghuvanshi_1279d6"/>
    <language>en</language>
    <item>
      <title>javascript nodejs npm typescript</title>
      <dc:creator>Yuvraj Raghuvanshi</dc:creator>
      <pubDate>Thu, 03 Apr 2025 05:38:40 +0000</pubDate>
      <link>https://dev.to/yuvraj_raghuvanshi_1279d6/javascriptnodejsnpmtypescript-2g2h</link>
      <guid>https://dev.to/yuvraj_raghuvanshi_1279d6/javascriptnodejsnpmtypescript-2g2h</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh" class="crayons-story__hidden-navigation-link"&gt;Simplify Environment Variable Validation in Node.js with env-valid&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/yuvraj_raghuvanshi_1279d6" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3010601%2Ffa8e76fd-2288-42d4-903c-701e90cfb31f.jpg" alt="yuvraj_raghuvanshi_1279d6 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/yuvraj_raghuvanshi_1279d6" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Yuvraj Raghuvanshi
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Yuvraj Raghuvanshi
                
              
              &lt;div id="story-author-preview-content-2377235" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/yuvraj_raghuvanshi_1279d6" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3010601%2Ffa8e76fd-2288-42d4-903c-701e90cfb31f.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Yuvraj Raghuvanshi&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Apr 3 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh" id="article-link-2377235"&gt;
          Simplify Environment Variable Validation in Node.js with env-valid
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;1&lt;span class="hidden s:inline"&gt; reaction&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            2 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>javascript</category>
      <category>node</category>
      <category>npm</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Simplify Environment Variable Validation in Node.js with env-valid</title>
      <dc:creator>Yuvraj Raghuvanshi</dc:creator>
      <pubDate>Thu, 03 Apr 2025 05:34:42 +0000</pubDate>
      <link>https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh</link>
      <guid>https://dev.to/yuvraj_raghuvanshi_1279d6/simplify-environment-variable-validation-in-nodejs-with-env-valid-58jh</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Problem statement&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Environment variables are critical for configuring Node.js apps—think API keys, ports, or database URLs. But manually validating them is error-prone and tedious. What if you could define a schema, enforce types, and catch issues early? That’s where env-valid comes in—a lightweight, TypeScript-friendly validator I built to make this process painless.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Setup&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;: Node.js installed, basic project setup.&lt;br&gt;
&lt;strong&gt;Installation&lt;/strong&gt;:&lt;br&gt;
bash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install env-valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Basic Project&lt;/strong&gt;:&lt;br&gt;
Create a new folder, run npm init -y, and add a .env file (optional, since env-valid works with process.env directly).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example .env&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PORT=3000
API_KEY=secret123
DEBUG=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: "You don’t need dotenv—env-valid reads process.env by default, but it pairs well with it if you prefer."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { createEnvValidator } = require('env-valid');

const env = createEnvValidator({
  PORT: { type: 'number', default: 3000 },
  API_KEY: { type: 'string', required: true }
});

const express = require('express');
const app = express();

app.get('/', (req, res) =&amp;gt; {
  res.send(`API Key: ${env.API_KEY}`);
});

app.listen(env.PORT, () =&amp;gt; {
  console.log(`Server running on port ${env.PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Advanced Usage&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const env = createEnvValidator({
  DEBUG: { type: 'boolean', default: false },
  HOSTS: { type: 'array' } // e.g., "a,b,c" or '["a", "b", "c"]'
});
console.log(env.DEBUG); // true/false based on "true", "yes", "1", etc.
console.log(env.HOSTS); // ["a", "b", "c"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Custom Validation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const env = createEnvValidator({
  DB_URL: {
    type: 'string',
    validate: (val) =&amp;gt; val.startsWith('mongodb://') || 'Must be a MongoDB URL'
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;TypeScript Support&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createEnvValidator } from 'env-valid';

const env = createEnvValidator({
  PORT: { type: 'number' as const, default: 3000 },
  API_KEY: { type: 'string' as const, required: true }
});

// Type-safe: env.PORT is number, env.API_KEY is string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"Full TypeScript support with autocompletion and type checking."&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use env-valid?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Unlike dotenv, it validates types and schemas. Compared to envalid, it’s lighter and TypeScript-first. It’s flexible—use it standalone or with existing setups."&lt;/li&gt;
&lt;li&gt;"Features like custom transforms, min/max constraints, and nested object validation make it versatile."&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion and call to action&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"With env-valid, you can stop worrying about invalid or missing env vars and focus on building your app. It’s open-source, so check it out on npm or GitHub!"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Links&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Npm&lt;/strong&gt;: &lt;a href="https://www.npmjs.com/package/env-valid" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/env-valid&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a href="https://github.com/yuvrajraghuvanshi/env-valid" rel="noopener noreferrer"&gt;https://github.com/yuvrajraghuvanshi/env-valid&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What do you think? Try it in your next project and let me know your feedback—issues and PRs welcome!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
