Skip to content

Conversation

DreaminDani
Copy link
Contributor

@DreaminDani DreaminDani commented Feb 19, 2022

Vite doesn't use process. it's import.meta

Fixes process not defined errors when using environment variables

@bluwy edit: Explain that import.meta.env. is used for build time.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

VITE doesn't use `process.` it's `import.meta`
@changeset-bot
Copy link

changeset-bot bot commented Feb 19, 2022

⚠️ No Changeset found

Latest commit: 69a4e1d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Rich-Harris
Copy link
Member

import.meta.env is for build time, not run time — I think the existing docs are correct? (Notwithstanding the fact that not every environment exposes process, of course)

@Conduitry
Copy link
Member

This is code that, on the server side, isn't changed by Vite at all. It's just process.env.whatever which Vite leaves alone and Node then interprets as it normally would, at runtime.

@Conduitry
Copy link
Member

Whoops, I'm on the mobile app and I missed Rich's comment. Yes the import.meta thing is replaced at build time but only for env vars beginning with VITE_. If you want runtime stuff, Vite is not involved.

@DreaminDani
Copy link
Contributor Author

Interesting. The FAQ is a bit confusing then... If I'm using an environment variable in a .js file, process isn't defined:

/* supabaseClient.js */
import { createClient } from '@supabase/supabase-js';

const supabaseUrl = process.env.VITE_SUPABASE_URL;
const supabaseAnonKey = process.env.VITE_SUPABASE_ANON_KEY;

export const supabase = createClient(supabaseUrl, supabaseAnonKey);
/* src/routes/auth/index.js */
<script>
	import { supabase } from '$lib/supabaseClient';

	let loading = false;
	let email;

	console.log(supabase);
</script>

image

But if I change process to import.meta, it runs successfully. I'm assuming this means I can only access process from a .svelte file?

@bluwy
Copy link
Member

bluwy commented Feb 20, 2022

Yeah I think the above paragraph "Vite uses dotenv..." could have mention the import.meta.env.* syntax as that's for build-time replacements. The paragraph later explains for runtime replacements.

I'm assuming this means I can only access process from a .svelte file?

You should be using import.meta.env in your example code. process.env should only be used in server code as that's determined in runtime, as in the browser runtime there's no process.

@DreaminDani
Copy link
Contributor Author

DreaminDani commented Feb 20, 2022 via email

@bluwy
Copy link
Member

bluwy commented Feb 21, 2022

Updated the docs to cover the discussion we had. Ready for a quick review.

@Rich-Harris Rich-Harris merged commit a8a3b14 into sveltejs:master Feb 21, 2022
@Rich-Harris
Copy link
Member

much better than what we had before 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants