npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@socheatsok78/dotenv-env

v0.1.4

Published

[![npm-badge][npm-badge]][npm-url]

Downloads

51

Readme

@socheatsok78/dotenv-env

npm-badge

Enhance dotenv with typescript declaration file.
Work in both JavaScript and TypeScript project with minimal configurations.

Installation

npm install @socheatsok78/dotenv-env
# or
yarn add @socheatsok78/dotenv-env

Usage

Simply add a tsconfig.json or jsconfig.json to indicates that the directory is the root of a TypeScript or JavaScript project.

See https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Here a minimal configurations:

Example jsconfig.json:

{
  "include": [
    "types/**/*"
  ],
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "moduleResolution": "node",
    "esModuleInterop": false,
    "baseUrl": ".",
    "paths": {
      //
    }
  }
}

Example tsconfig.json:

{
  "include": [
    "types/**/*"
  ],
  "compilerOptions": {
    "target": "es5",
    "strict": true,
    "module": "es2015",
    "moduleResolution": "node"
  }
}

Using with node

There is no other configuration needed for working with nodejs.

// types/dotenv.d.ts
import { NodeEnvironment } from '@socheatsok78/dotenv-env'

declare module "@socheatsok78/dotenv-env" {
  interface NodeEnvironment {
    /**
     * This will be one of "development", "production" or "test" depending on the mode the app is running in
     */
    NODE_ENV: string
  }
}

Then within your project e.g. index.js

// main.js
import { env } from '@socheatsok78/dotenv-env'

const mode = env(NODE_ENV, 'development')

Using with browser

For this to work in the browser you'll need to use bundler such as webpack, rollup or others for this to work.

Here an example usage with vuejs project. This will work out-of-the-box since @vue/cli uses webpack as its bundler.

First, update your .env file.

# .env
NODE_ENV=development

VUE_APP_MY_ENV_VARIABLE=value
VUE_APP_ANOTHER_VARIABLE=value

Create a new typescript's type declaration dotenv.d.ts in types folder.

// types/dotenv.d.ts
import { NodeEnvironment } from '@socheatsok78/dotenv-env'

declare module "@socheatsok78/dotenv-env" {
  interface NodeEnvironment {
    /**
     * This will be one of "development", "production" or "test" depending on the mode the app is running in
     */
    NODE_ENV: string

    /**
     * This corresponds to the publicPath option in `vue.config.js` and is the base path your app is deployed at
     */
    BASE_URL: string

    VUE_APP_MY_ENV_VARIABLE: string
    VUE_APP_ANOTHER_VARIABLE: string
  }
}

Then within your project e.g. main.js

// main.js
import { env } from '@socheatsok78/dotenv-env'

const MY_ENV_VARIABLE = env(VUE_APP_MY_ENV_VARIABLE, 'default')

Preview

Licensed

Licensed under MIT License.