Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

381
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda