Home > database >  process.env.NODE_ENV suddenly UNDEFINED in current SvelteKit project
process.env.NODE_ENV suddenly UNDEFINED in current SvelteKit project

Time:07-22

Inside svelte.config.js I was using this

const dev = process.env.NODE_ENV === 'development';

to conditionally set a base path which was working fine in projects with @sveltejs/[email protected] and *.357

After installing now the most recent SvelteKit version @sveltejs/[email protected] it only results to undefined

Differences I notice is that the new project lists "vite": "^3.0.0" as devDependency and the script changed from "dev": "svelte-kit dev", to "dev": "vite dev"

Update: It's also the case for a project with @sveltejs/[email protected], [email protected], "dev": "vite dev" - so the switch was before vite 3.0

Going through the vite docs I find import.meta.env, but that's also undefined inside svelte.config.js

Switching from Node v16 to 17 didn't make a difference as well

What changed and how can I now distinguish between dev and build mode?

CodePudding user response:

The behavior you describe was introduced in 100-next384

[breaking] remove mode, prod and server from $app/env (#5602)

and here is the relevant discussion

respect --mode, and remove server, prod and mode from $app/env

I think you should use Vite capabilities to configure dev VS production/build modes.

Update

Considering better your case, a way to solve the issue is to set a value for the NODE_DEV environment variable, like (Linux/Mac):

export NODE_ENV=development && npm run dev

There are other ways to do this but at least for development it should do the trick.

  • Related