Skip to content
Discussion options

You must be logged in to vote

await import(...) is executed by Node.js, not by Vite. When Node runs this line, it immediately tries to resolve and load the module. If the module is not found, Node will throw an error.
To handle this safely, you just need to wrap your await import("my-optional-dependency") call in a try...catch block.
Alternatively, you can use vite build -m <mode> instead of vite build, and handle the import conditionally inside defineConfig(({ command = "serve", mode }) => { ... }) to dynamically import different modules depending on the environment.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rthouvenin
Comment options

Answer selected by rthouvenin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants