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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@elfennol/env-to-js

v1.0.2

Published

Read runtime config injected as JSON script tags, with full TypeScript typing

Readme

@elfennol/env-to-js

Read runtime config injected as <script type="application/json"> tags, with full TypeScript typing.

Designed to work with elfennol/env-to-js-symfony-bundle, but usable with any backend that injects JSON script tags.

Installation

npm install @elfennol/env-to-js

Usage

import { createConfigReader } from '@elfennol/env-to-js';

type AppConfig = {
  apiBaseUrl: string;
  cdnBaseUrl: string;
};

const getAppConfig = createConfigReader<AppConfig>('#rc-app');

getAppConfig('apiBaseUrl');  // string
getAppConfig('cdnBaseUrl');  // string

The selector passed to createConfigReader must match the id attribute of the injected <script> tag:

<script type="application/json" id="rc-app">{"apiBaseUrl":"https://api.example.com"}</script>

API

createConfigReader<T>(selector)

Returns a typed getter function for the config block identified by selector.

  • Parses the JSON on the first key access, then caches the result for the lifetime of the reader instance.
  • Multiple readers are fully independent — each has its own cache.
const getAppConfig = createConfigReader<AppConfig>('#rc-app');
const getMapConfig = createConfigReader<MapConfig>('#rc-map');

EnvToJsError

Thrown when the element is not found or the JSON is invalid. The message is actionable:

EnvToJsError: element "#rc-app" not found. Make sure a <script type="application/json"> tag matching this selector is present in your HTML.
EnvToJsError: failed to parse JSON in element "#rc-app": Unexpected token

These are configuration errors — they indicate a missing or malformed script tag, not a runtime condition to recover from. Let them bubble up so they are visible immediately.

For advanced use cases (centralised logging, error monitoring), instanceof EnvToJsError lets you distinguish them from other errors:

import { createConfigReader, EnvToJsError } from '@elfennol/env-to-js';

window.addEventListener('error', (event) => {
  if (event.error instanceof EnvToJsError) {
    reportToMonitoring(event.error.message);
  }
});

With the Symfony bundle

Once elfennol/env-to-js-symfony-bundle is installed and env_to_js_scripts() is added to your base template, the bundle injects a <script> tag per provider on each request:

<script type="application/json" id="rc-app">{"apiBaseUrl":"https://api.example.com"}</script>

Read it in TypeScript:

import { createConfigReader } from '@elfennol/env-to-js';

const getAppConfig = createConfigReader<{ apiBaseUrl: string }>('#rc-app');

fetch(getAppConfig('apiBaseUrl') + '/users');

Requirements

  • TypeScript >= 5.0
  • Zero dependencies
  • Compatible with any bundler (Vite, Webpack, esbuild, Rollup) and vanilla JS

Development

npm run qa            # lint + typecheck + test
npm run lint          # ESLint only
npm run typecheck     # TypeScript type check only
npm test              # jest only

License

MIT