Skip to content

rt2yrru/bun-faker-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Faking Node.js with Bun

The Scenario

Bun is approximately 90% compatible with Node.js while being nearly 10x faster. Instead of running Node.js directly, we can use Bun and make it pretend to be Node.js so other programs can use it while thinking they're running Node.js.

How It Works

This approach uses binary wrapper scripts to make Bun masquerade as Node.js. The scripts create intelligent wrappers that intercept version checks and forward all actual work to Bun. Most of the heavy lifting is done by Bun itself since it's designed to be Node.js-compatible. The scripts only make Bun pretend to be Node.js via binary manipulation.

Does It Actually Work?

Yes, it works! And most of the time it's Bun doing the pretending on its own. These scripts only help Bun pretend to be Node.js through wrapper manipulation and intelligent version reporting.

Real-World Example

Prettify for Sublime Text was complaining when Bun was installed but Node.js wasn't. After using this faker script, Prettify started working flawlessly without any complaints. The scripts pretend to be the latest or LTS version of Node.js by fetching real version numbers from the internet and caching them.

The Implementation

The solution consists of three wrapper scripts that replace the standard Node.js binaries:

1. Node Wrapper (node.sh)

Creates /usr/local/bin/node that:

  • Fetches the latest and LTS Node.js versions from nodejs.org
  • Caches version numbers locally (updates every 25 hours)
  • Reports authentic Node.js versions when tools check node --version
  • Falls back to Bun's internal Node version if offline
  • Executes all actual commands using Bun

Smart Features:

  • Automatic version cache updates in background
  • Multi-tier fallback: Internet Latest → Internet LTS → Cached Latest → Cached LTS → Bun's version
  • Handles both --version and --lts-version flags
  • Seamlessly forwards all other commands to Bun

2. NPM Wrapper (npm.sh)

Creates /usr/local/bin/npm that:

  • Fetches the latest npm version from the npm registry
  • Caches version information (updates daily)
  • Reports authentic npm versions
  • Uses Bun's native npm compatibility for all commands

Smart Features:

  • Background cache updates (every 24 hours)
  • Connects to npm registry with 2-second timeout
  • Fallback to cached version (10.9.0 if cache missing)
  • All actual npm operations handled by Bun

3. NPX Wrapper (npx.sh)

Creates /usr/local/bin/npx that:

  • Uses the same npm version from cache
  • Reports matching npx version
  • Uses Bun's bun x for npx functionality

Smart Features:

  • Shares version cache with npm
  • Converts npx commands to bun x
  • Maintains version consistency

Installation

Simply run the three scripts in order:

# Run node wrapper
bash node.sh

# Run npm wrapper  
bash npm.sh

# Run npx wrapper
bash npx.sh

That's it! The scripts handle everything automatically.

Verification

Test the installation:

# Check versions
node --version          # Shows latest Node.js version
node --lts-version      # Shows LTS version
npm --version           # Shows latest npm version
npx --version           # Shows matching npx version

# Check binary locations
which node              # /usr/local/bin/node
which npm               # /usr/local/bin/npm
which npx               # /usr/local/bin/npx

# Verify it's actually Bun underneath
node -e "console.log('It works!')"

What Works

  • Sublime Text Prettify (confirmed working)
  • Version checks - Reports real Node.js/npm/npx versions
  • Most npm packages and scripts
  • Package.json scripts - Everything runs through Bun
  • Development tools expecting Node.js
  • Build tools and bundlers
  • Offline operation - Uses cached versions when internet unavailable
  • Automatic updates - Versions stay current without manual intervention

What Might Not Work

  • ❌ Native Node.js modules with C++ bindings (sometimes)
  • ❌ Tools that directly check Node.js binary signatures
  • ❌ Some edge cases in the 10% incompatibility range
  • ❌ Very old tools expecting ancient Node.js behavior

How Version Caching Works

The scripts are intelligent about version reporting:

Node Wrapper:

  • Checks nodejs.org/dist/index.json for latest versions
  • Updates cache every ~25 hours (90000 seconds)
  • Stores both latest and LTS versions
  • Falls back gracefully when offline

NPM Wrapper:

  • Checks registry.npmjs.org for latest npm version
  • Updates cache every 24 hours (1440 minutes)
  • Runs update in background (non-blocking)
  • Defaults to 10.9.0 if no cache exists

NPX Wrapper:

  • Shares npm's version cache
  • Maintains consistency with npm version
  • No separate network calls needed

Performance Benefits

Since Bun is ~10x faster than Node.js, you get:

  • Faster script execution - Everything runs quicker
  • Quicker package installations - Bun's package manager is blazing fast
  • Improved build times - Builds complete faster
  • Better development experience - Less waiting, more coding
  • Lower resource usage - Bun is more efficient

Reverting Back

To remove the faker and restore real Node.js:

# Remove wrapper scripts
sudo rm /usr/local/bin/node
sudo rm /usr/local/bin/npm
sudo rm /usr/local/bin/npx

# Clean up cache files (optional)
rm ~/.node_version_cache
rm ~/.npm_version_cache

# Install real Node.js
# Use your preferred method (nvm, apt, brew, etc.)

Technical Details

User Detection:

  • Scripts properly detect the real user even when run with sudo
  • Home directory resolution works correctly for all scenarios
  • Cache files stored in user's home, not root

Path Resolution:

  • Uses ~/.bun/bin/bun for Bun binary location
  • Works with standard Bun installation paths
  • Handles both sudo and non-sudo execution

Error Handling:

  • Network timeouts prevent hanging
  • Graceful fallbacks at every level
  • Silent failures don't break functionality

Notes

  • The scripts create wrapper executables, not symbolic links
  • Version information stays current automatically
  • Most tools never know they're running on Bun instead of Node.js
  • Cache updates happen in background (non-blocking)
  • Works offline with cached versions
  • Bun does the actual work - scripts just handle the facade

Disclaimer

This is a compatibility hack. While it works for many use cases (like Sublime Text Prettify and most development tools), it's not a complete replacement for Node.js. Use in development environments and test thoroughly before deploying to production.

The 90% compatibility means some edge cases may fail. When in doubt, test your specific use case first.


Remember: It's Bun doing the heavy lifting. These scripts just make the introduction! 🚀

About

fake bun to pretend like nodejs

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages