Socket
Book a DemoInstallSign in
Socket

castenv

Package Overview
Dependencies
Maintainers
0
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

castenv

Automatically cast process.env environment variables into native types (boolean, number, json object)

0.1.18
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
0
Weekly downloads
 
Created
Source

castenv

Build Status Downloads Downloads npm version dependencies dev dependencies License

Overview

Environment variables like MAX_THREADS=5 , ENABLE_DEBUG=false are usually imported into process.env as strings. This leads to subtle bugs when treating them like booleans or numbers e.g. if (process.env['ENABLE_DEBUG'])... because "false" equals true in JS

castenv uses JSON.parse() to convert env variables into native types. castenv.cast() will modify process.env, and castenv.env() will leave process.env unmodified.

Getting Started

Install it via npm:

npm install castenv

Include in your project:

// cast process.env in place
require("castenv").cast()

// alternatively, don't modify process.env
var env = require("castenv").env()

Example Casting

process.env["TEST_NUMBER_VALUE"] = "5"
console.log(typeof process.env["TEST_NUMBER_VALUE"])
// "string"
require("castenv").cast()
console.log(typeof process.env["TEST_NUMBER_VALUE"])
// number

Pitfalls

  • hex & octal values like 0x123 and 0123 will return as strings--JSON doesn't support those literals

License

MIT

FAQs

Package last updated on 09 Jul 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.