Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
How to manually throw a webpack error from the code on a condition like a missing variable?

I have some code where I have placeholders. There are some variables that I don't know yet that I need to fill in when the code is pushed to production. Right now I have the following in a file called constants.js.

export const PublicAddress = process.env.PUBLIC_ADDRESS || console.error( "Public address is not yet set." );

This code works just fine and there will be an error logged to the console when PUBLIC_ADDRESS is empty.

However, if PUBLIC_ADDRESS was empty AND I forgot to hardcode another value, I'd like Webpack to throw an error when compiling instead. It's really important that I don't forget to put in a real value when I compile it for production.

Is that possible?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's typically this kind of check that you can provide in your own webpack plugin. here is a basic implementation of a custom plugin that check if all string from an array refer to an env variable :

class EnvVarExistPlugin {
  apply(compiler) {
    const envsVarToCheck = ['TEST_VAR', 'TEST_VAR2'] // put your env var list here
    envsVarToCheck.forEach(envVar => {
      if (!!!process.env[envVar]) {
        throw new Error(`Environment variable : ${envVar} is missing`);
      }
    });
  }
 };

 module.exports = EnvVarExistPlugin

Then register your plugin in your webpack.config.js :

const { resolve } = require("path");
const EnvVarExistPlugin = require("./pathToYourPlugin");
require('dotenv').config({ path: './.env' }); // adapt path to your env file

module.exports = {
 entry: resolve(__dirname, "src/index.js"),
 mode: 'development',
 output: {
   path: resolve(__dirname, "dist"),
   filename: "bundle.js"
 },
 plugins: [new EnvVarExistPlugin()]
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!