Socket
Socket
Sign inDemoInstall

dotenv-tool

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dotenv-tool

A tool to manipulate `.env` files


Version published
Weekly downloads
75K
increased by9.35%
Maintainers
1
Install size
39.7 kB
Created
Weekly downloads
 

Readme

Source

DotEnv Tool

A tool to manipulate .env files format.

Create a .env file programmatically

import DotEnvTool from 'dotenv-tool'

const tool = new DotEnvTool({
  path: '/path/to/your/.env'
})

tool.addComment('# The server url')
tool.addKey('SERVER_URL', 'http://0.0.0.0:3042')
tool.addBlankLine()
tool.addComment('# The database connection string')
tool.addKey('CONNECTION_STRING', 'sqlite://./db.sqlite')

await tool.save()

This will save /path/to/your/.env file like this

# The server url
SERVER_URL=http://0.0.0.0:3042

# The database connection string
CONNECTION_STRING=sqlite://./db.sqlite

Load and manipulate an existing .env file

import DotEnvTool from 'dotenv-tool'

const tool = new DotEnvTool({
  path: join(__dirname, 'fixtures', 'sample.env')
})
await tool.load()

tool.updateLine(3, { type: 'comment', value: '# a new comment' })

await tool.save()

Get and set keys

import DotEnvTool from 'dotenv-tool'

const tool = new DotEnvTool({
  path: join(__dirname, 'fixtures', 'sample.env')
})
await tool.load()

if (!tool.hasKey('SERVER_URL')) {
  tool.addKey('SERVER_URL', 'http://0.0.0.0:3042')

  if (process.env.IS_PROD) {
    tool.updateKey('SERVER_URL', 'http://production.app')
  }
}

await tool.save()

Keywords

FAQs

Last updated on 20 Mar 2024

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc