Skip to content

@fkn/vite-plugin

@fkn/vite-plugin is the one-line way to run Node-style code in the browser on FKN. It aliases the Node builtins to the matching @fkn/lib entry points, so supported fs, net, dgram, and http calls work in browser bundles. Storage uses FKN storage; networking uses the cloud-backed socket APIs.

vite.config.ts
import { defineConfig } from 'vite'
import { fkn } from '@fkn/vite-plugin'
export default defineConfig({
plugins: [fkn()],
})

With that in place, bare Node imports resolve to the platform:

import fs from 'fs'
import net from 'net'
fs.writeFileSync('/save/state.json', JSON.stringify({ ok: true }))
const socket = net.connect(443, 'example.com')
  • fs and fs/promises to @fkn/lib/fs and @fkn/lib/fs/promises, the same implementation described by the Node fs polyfill guide
  • net to @fkn/lib/net, Node-compatible TCP over WebVPN
  • dgram to @fkn/lib/dgram, Node-compatible UDP over WebVPN
  • http to @fkn/lib/http, the HTTP client built on the socket layer

It matches the node: forms too (node:fs, node:net, node:dgram, node:http), and uses exact-match resolution so a package like fs-extra is left alone. Every other Node builtin (stream, events, buffer, …) falls back to node-stdlib-browser, and the plugin injects the Buffer / process / global shims the polyfills expect.

Each alias is on by default; pass false to drop one:

import { fkn } from '@fkn/vite-plugin'
fkn({ net: false, dgram: false, http: false }) // alias fs only

This lets libraries using the supported Node-compatible surface keep their existing imports. A BitTorrent engine like libtorrent-wasm opens net and dgram sockets through those imports, and the plugin routes them through FKN.