Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

374
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda