Node.js
Milkio can freely switch JavaScript runtimes and can run in Node.js.
Node.js is currently the most widely used JavaScript runtime, accompanying JavaScript throughout its growth.
Installing Node.js
You can go to this link to install Node.js, or use nvm or nvm-windows to easily switch between versions.
After installation, by running the following command, if the version number is displayed successfully, it means your Node.js has been installed successfully.
Using Node.js
Node.js does not have built-in support for running TypeScript files directly, we can achieve this by installing vite-node.
Edit your /package.json
file, replace the commands using Bun to run with the commands using vite-node.
Since using the built-in HTTP server in Node.js can be complex, we recommend using Hattip to simplify this process.
Edit your /run-serve.ts
file, change the code for starting the HTTP server in default mode to use Hattip.
Deployment
We don’t need to install bundling tools like Rollup or ESBuild because Bun itself supports bundling code for Node.js and is faster than ESBuild.
Modify our /package.json
file, replace the build
command in the scripts
section with:
When we run npm run build
, our code will be bundled into the dist
directory. Copy the directory to your server, and you can run it directly with Node.js:
If it doesn’t run, try renaming the file to run-serve.mjs
.
Details
By default, Bun bundles all your code into a single .js
file, which can cause issues with lazy loading and accessing methods or variables from third-party packages due to the disrupted execution order. When bundling, we added the --splitting
command, which allows our files to be truly lazily loaded, especially useful when your project becomes very large.