Installations
npm install vite-plugin-node
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.13.0
NPM Version
8.19.3
Score
53.2
Supply Chain
89.6
Quality
76.9
Maintenance
100
Vulnerability
95.8
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
axe-me
Download Statistics
Total Downloads
1,938,093
Last Day
1,399
Last Week
19,809
Last Month
134,092
Last Year
1,154,566
GitHub Statistics
1,017 Stars
72 Commits
48 Forks
12 Watching
1 Branches
8 Contributors
Package Meta Information
Latest Version
4.0.0
Package Id
vite-plugin-node@4.0.0
Unpacked Size
33.17 kB
Size
10.98 kB
File Count
33
NPM Version
8.19.3
Node Version
18.13.0
Publised On
09 Oct 2024
Total Downloads
Cumulative downloads
Total Downloads
1,938,093
Last day
-76.5%
1,399
Compared to previous day
Last week
-42.3%
19,809
Compared to previous week
Last month
-9.7%
134,092
Compared to previous month
Last year
86.3%
1,154,566
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Vite Plugin Node
A vite plugin to allow you to use vite as node dev server.
Features
- All the perks from Vite plus:
- Node server HMR! (hot module replacement)
- Support Express, Fastify, Koa and Nest out of the box
- Support Custom Request Adapter
- You can choose to use
esbuild
orswc
to compile your typescript files
Get started
-
Install vite and this plugin with your favorite package manager, here use npm as example:
1npm install vite vite-plugin-node -D
-
Create a
vite.config.ts
file in your project root to config vite to actually use this plugin:1import { defineConfig } from 'vite'; 2import { VitePluginNode } from 'vite-plugin-node'; 3 4export default defineConfig({ 5 // ...vite configures 6 server: { 7 // vite server configs, for details see [vite doc](https://vitejs.dev/config/#server-host) 8 port: 3000 9 }, 10 plugins: [ 11 ...VitePluginNode({ 12 // Nodejs native Request adapter 13 // currently this plugin support 'express', 'nest', 'koa' and 'fastify' out of box, 14 // you can also pass a function if you are using other frameworks, see Custom Adapter section 15 adapter: 'express', 16 17 // tell the plugin where is your project entry 18 appPath: './app.ts', 19 20 // Optional, default: 'viteNodeApp' 21 // the name of named export of you app from the appPath file 22 exportName: 'viteNodeApp', 23 24 // Optional, default: false 25 // if you want to init your app on boot, set this to true 26 initAppOnBoot: false, 27 28 // Optional, default: 'esbuild' 29 // The TypeScript compiler you want to use 30 // by default this plugin is using vite default ts compiler which is esbuild 31 // 'swc' compiler is supported to use as well for frameworks 32 // like Nestjs (esbuild dont support 'emitDecoratorMetadata' yet) 33 // you need to INSTALL `@swc/core` as dev dependency if you want to use swc 34 tsCompiler: 'esbuild', 35 36 // Optional, default: { 37 // jsc: { 38 // target: 'es2019', 39 // parser: { 40 // syntax: 'typescript', 41 // decorators: true 42 // }, 43 // transform: { 44 // legacyDecorator: true, 45 // decoratorMetadata: true 46 // } 47 // } 48 // } 49 // swc configs, see [swc doc](https://swc.rs/docs/configuration/swcrc) 50 swcOptions: {} 51 }) 52 ], 53});
-
Update your server entry to export your app named
viteNodeApp
or the name you configured.
ExpressJs
1const app = express(); 2 3// your beautiful code... 4 5if (import.meta.env.PROD) 6 app.listen(3000); 7 8export const viteNodeApp = app;
More Examples:
-
Add a npm script to run the dev server:
1"scripts": { 2 "dev": "vite" 3},
-
Run the script!
npm run dev
Custom Adapter
If your favorite framework not supported yet, you can either create an issue to request it or use the adapter
option to tell the plugin how to pass down the request to your app. You can take a look how the supported frameworks implementations from the ./src/server
folder.
Example:
1import { defineConfig } from 'vite'; 2import { VitePluginNode } from 'vite-plugin-node'; 3 4export default defineConfig({ 5 plugins: [ 6 ...VitePluginNode({ 7 adapter({ app, server, req, res, next }) { 8 app(res, res); 9 }, 10 appPath: './app.ts' 11 }) 12 ] 13});
Build
This plugin leverages Vite SSR mode to build your app. All you need to do is add a build script to your package.json:
1"scripts": { 2 "build": "vite build" 3},
For more build config please check vite doc
Note: By default, starting from vite v3, the ssr buildle will be in esm format. if you want to build cjs, add
ssr: { format: 'cjs' }
to your vite config.
Examples
See the examples folder.
Why?
While frontend development tooling is evolving rapidly in recent years, backend DX is still like in stone age. No hot module replacement; Typescript recompiling slow as funk; Lack of plugin system etc. Thanks to Vite.js created by Evan You (A.K.A creator of vue.js; my biggest idol developer), makes all those dreams for backend development come true!
How?
Vite by design has a middleware mode, which allows us to use it programmatically inside other modules. It's originally made for SSR, so that for each request, vite can load the renderer and render the latest changes you made to your app (https://vitejs.dev/guide/ssr.html). This plugin leverages this feature to load and execute your server app entry.
You may ask, isn't super slow, since it re-compiles/reloads the entire app? The answer is NO, because vite is smart. It has a builtin module graph as a cache layer, the graph is built up the first time your app loads. After that, when you update a file, vite will only invalidate that one and its parent modules, so that for next request, only those invalidated modules need to be re-compiled which is super fast thanks to esbuild or swc.
To-Do
As this plugin just fresh developed, there are still lots ideas need to be implemented, including:
- Test with a large node project, I need y'all helps on this!
- Unit tests
Bugs
Please create an issue if you found any bugs, to help me improve this project!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 7/22 approved changesets -- score normalized to 3
Reason
1 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 2
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 15 are checked with a SAST tool
Reason
24 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-h452-7996-h45h
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-wm7h-9275-46v2
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-3fjj-p79j-c9hh
- Warn: Project is vulnerable to: GHSA-9pv7-vfvm-6vr7
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to vite-plugin-node
vite-plugin-node-polyfills
A Vite plugin to polyfill Node's Core Modules for browser environments.
vite-plugin-node-stdlib-browser
Configure `node-stdlib-browser` for Vite projects
rollup-plugin-node-externals
Automatically declare NodeJS built-in modules and npm dependencies as 'external' in Rollup config
vite-plugin-files-copy
vite plugin copy