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

crenv

v0.9.8

Published

Performs the loading and reading of cross-side environment variables at runtime, on the server and in the browser, including generated static HTML pages.

Downloads

14

Readme

Basic usage

$ crenv --target .env.production --public ./src/public -- [your after command here]

A file will be generated on your defined public path (./src/public/envs.js) with the variables that can be accessed on the client like this. You will just need to import the auto-generated file into your project, example:

<script src="public/envs.js"></script>

This is especially useful for SSR projects like those written in REACT and VUE where variables are loaded during build or when pages are rendered client-side and server-side.

This way we can access the client and server side variables automatically like this:

import env from "crenv";

// read client and server side environments
env.APPLICATION_ENV_HELLO

Then you no longer need to worry about dealing with process.env or knowing how and when you will need to access the environment variables in the client, everything will be done cross-environment and automatically.

Installation

Installation is easy with the npm command:

$ npm install --save crenv

If you only need the cli outside the project, you can install the lib globally:

$ npm install -g crenv

cli

The cli allows the automatic generation of environment variables looking directly at the dotenv file which may vary in each your application environment.

Example: As long as you have an .env file:

// .env.production
MY_CROSS_ENV_ENVIRONMENT=production
MY_CROSS_ENV_API_URL=https://my-api.io
SECURITY_SERVER_KEY=88370f41-9204-43df-91a5-b089c6b3e89b

Some of your variables may need to be accessed server-side or client-side. When running:

$ crenv -e production --prefix MY_CROSS_ENV

A file will be generated with cross environments variables defineds with prefix MY_CROSS_ENV in its default public folder (./public/envs.js) and the client can consume it by importing the envs.js file into the page.

// public/envs.js

window.env = {
    MY_CROSS_ENV_ENVIRONMENT: "production",
    MY_CROSS_ENV_API_URL: "https://my-api.io"
}

Your project will automatically handle variables when you access them this way. On ssr pages for example, they will work in server-side as well as client-side rendering.

import env from "crenv"

export default () => (
  <div>
    <h1>My environment {env.MY_CROSS_ENV_ENVIRONMENT}</h1>
  </div>
);
// API client integration file
import env from "crenv"

export const getPosts = () => fetch(`${env.MY_CROSS_ENV_API_URL}/posts`);

OBS.:

  • If no target dotenv file is defined or no environment is defined the crenv will read the environment variables from where it is running, from process.env.
  • You need to define a prefix before running the command. If not defined, the default prefix is ​​APPLICATION_ENV. Only variables with the defined prefix are loaded, as in the example above.

cli options

| Command | Description | |-----------------------|-----------------------------------------------------------------------------------------------| | -e / --env | Set the target environment. The lib is read based on dotenv file suffix: .env.[environment]. | | -t / --target | Set target dotenv file: crenv -t ./.env.develop. | | --public | Set the public project path. When run the command, the envs.js will be generated in this path. The default path value is ./public/. | | --prefix | Set prefix environment variables. All variables with prefix will be understood as global and will also be passed to the client-side. On the server, all variables can be read through this module. On the client, only those with a defined prefix can be accessed. The default prefix is APPLICATION_ENV. | | -v / --version | Show current lib version | | -h / --help | Show help commands |