简体   繁体   English

当前 SvelteKit 项目中的 process.env.NODE_ENV 突然未定义

[英]process.env.NODE_ENV suddenly UNDEFINED in current SvelteKit project

Inside svelte.config.js I was using thissvelte.config.js里面我用的是这个

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

to conditionally set a base path which was working fine in projects with @sveltejs/kit@1.0.0-next.350 and *.357有条件地设置在@sveltejs/kit@1.0.0-next.350*.357项目中工作正常的基本路径

After installing now the most recent SvelteKit version @sveltejs/kit@1.0.0-next.386 it only results to undefined现在安装最新的 SvelteKit 版本@sveltejs/kit@1.0.0-next.386它只会导致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"我注意到的不同之处在于,新项目将"vite": "^3.0.0"列为 devDependency 并且脚本从"dev": "svelte-kit dev",更改为"dev": "vite dev"

Update: It's also the case for a project with @sveltejs/kit@1.0.0-next.366 , vite@2.9.14 , "dev": "vite dev" - so the switch was before vite 3.0更新: @sveltejs/kit@1.0.0-next.366 , vite@2.9.14 , "dev": "vite dev"的项目也是如此 - 所以切换是在 vite 3.0 之前

Going through the vite docs I find import.meta.env , but that's also undefined inside svelte.config.js通过 vite 文档,我找到了import.meta.env ,但这在svelte.config.js中也是undefined

Switching from Node v16 to 17 didn't make a difference as well从 Node v16 切换到 17 也没有什么不同

What changed and how can I now distinguish between dev and build mode?发生了什么变化,我现在如何区分dev模式和build模式?

The behavior you describe was introduced in 100-next384您描述的行为是在100-next384中引入的

[breaking] remove mode, prod and server from $app/env (#5602) [破坏] 从 $app/env (#5602) 中删除模式、产品和服务器

and here is the relevant discussion是相关的讨论

respect --mode, and remove server, prod and mode from $app/env尊重 --mode,并从 $app/env 中删除 server、prod 和 mode

I think you should use Vite capabilities to configure dev VS production/build modes.我认为您应该使用Vite 功能来配置开发 VS 生产/构建模式。

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):考虑到更好的情况,解决问题的一种方法是为NODE_DEV环境变量设置一个值,例如(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.还有其他方法可以做到这一点,但至少对于开发来说它应该可以解决问题。

This might help someone in the future.这可能会在将来帮助某人。 I encountered this error when I created an express server in a react application.我在 React 应用程序中创建快速服务器时遇到了这个错误。 I created the server directory in the root folder of the react application and I was running both react app and express server at the same time.我在 react 应用程序的根文件夹中创建了服务器目录,并且同时运行了 react app 和 express server。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM