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

nuxt-envalid

v0.0.6

Published

A Nuxt.js module thats validates your env variables and loads them cleaned into your application context

Downloads

16

Readme

Logo of nuxt-envalid

nuxt-envalid

CI CodeQL License NPM version

Dead simple Envalid integration for Nuxt 2.

Features

  • Define a required schema for your environment variables
  • Validates variables in the env property of the nuxt.config.js
  • Validates variables in process.env
  • Validates variables present in the .env file, if loaded together with @nuxtjs/dotenv
  • Fails the build process if a variable is missing
  • Loads them cleaned and enriched with default values into your application context (process.env and context.env)

Getting Started

  1. Add nuxt-envalid as dev-dependency to your project via yarn or npm:
yarn add --dev nuxt-envalid # or npm install --save-dev nuxt-envalid
  1. Add nuxt-envalid to the buildModules section of nuxt.config.js:
// nuxt.config.js
export default {
  buildModules: ['nuxt-envalid'],
};

:warning: If you are using a Nuxt version previous than v2.9 you have to install the module as a dependency (No --dev or --save-dev flags) and also use modules section in nuxt.config.js instead of buildModules.

Inline config

// nuxt.config.js
export default {
  buildModules: [
    [
      'nuxt-envalid',
      {
        /* module config */
      },
    ],
  ],
};

Top level config

// nuxt.config.js
export default {
  buildModules: ['nuxt-envalid'],
  envalid: {
    /* module config */
  },
};

Config function

If you need to use a function to provide the module config you are good to go:

// nuxt.config.js
export default {
  buildModules: [
    [
      'nuxt-envalid',
      () => ({
        /* module config */
      }),
    ],
  ],
  /* or at top level */
  envalid: () => ({
    /* module config */
  }),
};

:warning: Defining module options inline will overwrite module options defined at top level.

Configuration

Overview

| Param | Description | Required | Default | | ------------------ | ----------------------------------------------------------------------------- | -------- | ------- | | specs | An object that specifies the format of required vars. | No | | | options | An (optional) object, which supports the following key: | No | | | options.reporter | Pass in a function to override the default error handling and console output. | No | |

specs

For further information take a look at the official documentation of envalid.

// nuxt.config.js
import { bool, str } from 'nuxt-envalid';
export default {
  buildModules: ['nuxt-envalid'],
  envalid: {
    specs: {
      TITLE: str(),
      SUBTITLE: str({ default: 'subtitle' }),
      IS_PUBLIC: bool({ default: false }),
    },
  },
};

options

For further information take a look at the official documentation of envalid.

// nuxt.config.js
export default {
  buildModules: ['nuxt-envalid'],
  envalid: {
    options: {
      reporter: ({ errors, env }) => {
        console.log(errors, env);
      },
    },
  },
};

Usage

Usage with env property in Nuxt config

// nuxt.config.js
import { bool, host } from 'nuxt-envalid';
export default {
  env: {
    BACKEND_HOST: 'backend.example.com',
  },
  buildModules: ['nuxt-envalid'],
  envalid: {
    specs: {
      BACKEND_HOST: host(),
      BACKEND_SECURE: bool({ default: true }),
    },
  },
};
<!-- pages/index.vue -->
<template>
  <div>
    <h1>{ { post.title } }</h1>
    <p>{ { post.description } }</p>
  </div>
</template>

<script>
export default {
  async asyncData({ env }) {
    const response = await fetch(
      `${env.BACKEND_SECURE ? 'https' : 'http'}://${env.BACKEND_HOST}/post/1`
    );
    const post = await response.json();
    return { post };
  },
};
</script>

Using together with @nuxtjs/dotenv

This module will validate the result of @nuxtjs/dotenv.

:warning: Be sure to include this module AFTER @nuxtjs/dotenv.

# .env
CTF_CDA_ACCESS_TOKEN="super-secret-access-token"
// nuxt.config.js
import { str } from 'nuxt-envalid';
export default {
  env: {
    CTF_SPACE_ID: 'my-space-id',
  },
  buildModules: ['@nuxtjs/dotenv', 'nuxt-envalid'],
  envalid: {
    specs: {
      CTF_SPACE_ID: str(),
      CTF_CDA_ACCESS_TOKEN: str(),
      CTF_ENVIRONMENT: str({ default: 'production' }),
    },
  },
};
// plugins/contentful.js
import { createClient } from 'contentful';

export default createClient({
  space: process.env.CTF_SPACE_ID,
  accessToken: process.env.CTF_CDA_ACCESS_TOKEN,
  environment: process.env.CTF_ENVIRONMENT,
});

Accessing the data

Since this module is only there to validate the presence of environment variables and to load them sanitized into the already existing process.env and context.env, the general access of the data doesn't change. Take a look on the official documentation to get a deeper insight here.

Missing variables

Validation takes places during build time. So if any variable out of the specified configuration is missing in the env property of the Nuxt config or in the .env file, if @nuxtjs/dotenv is used, the build will fail.

License

MIT License