Rebuild native Node.js modules

Medium risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Recompiles installed native addons against the active Node.js version, fixing NODE_MODULE_VERSION mismatch errors after a Node.js upgrade.

nodenpmnativerebuild

Parameters

The native module to require as a smoke test after rebuilding, e.g. sqlite3.

Commands

Confirm which Node.js version the modules will be built against

node --version

Recompile all native addons for the active Node.js version

npm rebuild

Verification

node -e "require('<module_path>')"

The rebuilt native module loads without throwing a NODE_MODULE_VERSION or binding error.

Undo

Not undoable

Rebuilding replaces compiled binaries in node_modules; a clean reinstall restores the standard state.

Pitfalls

  • Rebuilding requires a working compiler toolchain (Xcode CLT on macOS, build-essential on Linux, Visual Studio Build Tools on Windows).
  • If errors persist, a clean reinstall of node_modules is more reliable than rebuilding in place.
  • Electron apps must rebuild against Electron's Node ABI instead, using @electron/rebuild.

Related