English 中文(简体)
SvelteKit标记代码中的环境变量无法在Vercel开展工作。
原标题:Environment variable in markup codes of SvelteKit could not work at Vercel

Hi~ I am a developer in South Korea. I am not good at english yet. But I have a question about SvelteKit. I wrote the below codes in +page.svelte as SvelteKit tutorial. This code worked well in local dev mode.

<script>
  import { env } from  $env/dynamic/public ;
</script>

<main style:background={env.PUBLIC_THEME_BACKGROUND} style:color={env.PUBLIC_THEME_FOREGROUND}>
  {env.PUBLIC_THEME_FOREGROUND} on {env.PUBLIC_THEME_BACKGROUND}
</main>
.....

Then I deployed the above codes to Vercel, build and published. But I got some error that is undefined in import { env } from $env/dynamic/public Other codes of the SvelteKit tutorial which was deployed to Vercel worked well except this code. That is, This error is not problem of the adapter or runtime of Vercel. Ok, I already changed them but could not fix this error.

因此,我设立了<代码>+page.server.ts文档,我将上述代码移至下文。

import { env } from  $env/dynamic/public ;

export function load() {
  return { env: { 
    PUBLIC_THEME_BACKGROUND: env.PUBLIC_THEME_BACKGROUND,
    PUBLIC_THEME_FOREGROUND: env.PUBLIC_THEME_FOREGROUND,
  }};
}

我将标记文档(+page.svelte)修改如下。

<script>
  export let data;
</script>

<main style:background={data.env.PUBLIC_THEME_BACKGROUND} style:color={data.env.PUBLIC_THEME_FOREGROUND}>
  {data.env.PUBLIC_THEME_FOREGROUND} on {data.env.PUBLIC_THEME_BACKGROUND}
</main>
.....

现在, 该法典运作良好。 简言之,为什么不能打上<条码>美元/动力/公共。 在Vercel的标记档案中工作?

  • Changed the adapter of Vercel in svelte.config.js from edge to nodejsXXX.
  • Changed the build script in package.json from vite build to mv ./env.local to ./.env && vite build and remove .env from .gitignore.
  • Set up Vercel s environment variables in Project Settings.
问题回答

Instead of reading your environment variables from your directory, have you considered using Vercel s environment variables? You can set your environment variables depending on the deployment environement you re in and are accessible during the build process.

“在此处的影像描述”/</a

You can call them in your code like this. enter image description here

此外,如果你想要一份当地环境变数的复印件,你就能够做到:

vercel env pull 

因此,贵重变量为何在您的标记档案中已有。 Vercel利用对冲功能处理各种要求,这些对冲功能在建时获得环境变量的机会有限。

缩略语 由于Vercel的边缘功能没有这种能力,在建筑期间,环境变量在你的标记档案中可以直接获得。





相关问题
Installing node-faiss & libomp when deploying

I m building AI website with Chagpt API & langchain. all using node.js, express now when I want to deploy the project on any platform, like vercel or renderjs I get this error telling me that ...

Intercept email with a Vercel application?

There are systems like zoho desk that allow you to configure an email as an input/relay for support tickets. So if an email is sent to support@x.com from user at user@z.com the system will "...

热门标签