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

373
Views
Replacing variable with @rollup/plugin-replace throws error

Problem

I am trying to inject / replace environment variables with @rollup/plugin-replace. Unfortunately, I get this error:

TypeError: can't convert undefined to object

Code

// rollup.config.js
import replace from "@rollup/plugin-replace";
import { config } from "dotenv";
config();

export default {
  // ...
  plugins: [
    replace({
      values: { YOUTUBE_API: JSON.stringify(process.env.YOUTUBE_API) },
      preventAssignment: true,
    }),
  // ...
}

And I call it like this:

  onMount(() => {
    (async function getPopular() {
      videos = await axios.get("https://www.googleapis.com/youtube/v3/videos", {
        part: "id, snippet, suggestions",
        chart: "mostPopular",
        key: YOUTUBE_API,
      });
    })();
  });

What I tried

I logged out the variable and so can confirm that it exists. Also, if I remove the stringify function, I get another error:

ReferenceError: blablabblub is not defined

I have done this successfully in other projects. What the heck is wrong here?

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

0

So after some digging around with the same issue, I found it was related to object assignment. For example:

export default {
  // ...
  plugins: [
    replace({
      values: {
        env: {
          API_URL: process.env.API_URL,
          API_VERSION: process.env.API_VERSION,
        }
      },
      preventAssignment: true,
    }),
  // ...
}

// in some JS or Svelte file
const config = {
    host: env.API_URL,
    version: env.API_VERSION
}

// The above will result in a reference error of 'env' not being defined. 

// in the same JS or Svelte file..
const envVars = env;
const config = {
    host: envVars.API_URL,
    version: envVars.API_VERSION
}

// this works just fine!

I haven't had anymore time to investigate, but my gut feeling is that rollup won't replace variable names when they are nested inside an object assignment. It might be nice for an optional flag to allow this, but it might also get very messy hence why they didn't do it.

I hope this helps if it's still an issue for you.

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!