External is very similar to Object Wrap, which is used in Class under the hood.

Object Wrap attaches a native value to a JavaScript Object and can notify you when the attached JavaScript Object is recycled by GC. External creates an empty, blank JavaScript Object which holds the native value under the hood. It only works by passing the object back to Rust:

use napi::bindgen_prelude::*;
use napi_derive::napi;
fn create_source_map(length: u32) -> External<Buffer> {
  External::new(vec![0; length as usize].into())


export class ExternalObject<T> {
  readonly '': {
    readonly '': unique symbol
    [K: symbol]: T
export function createSourceMap(length: number): ExternalObject<Buffer>

External is very useful when you want to return a JavaScript Object with some methods on it to interact with the native Rust code. Here is a real-world example:

impl MagicString {
  #[napi(ts_return_type = "{ toString: () => string, toUrl: () => string }")]
  pub fn generate_map(
    &mut self,
    options: Option<magic_string::GenerateDecodedMapOptions>,
  ) -> Result<External<SourceMap>> {
    let external = create_external(self.0.generate_map(options.unwrap_or_default())?);
  /// @internal
  pub fn to_sourcemap_string(&mut self, sourcemap: External<SourceMap>) -> Result<String> {
  /// @internal
  pub fn to_sourcemap_url(&mut self, sourcemap: External<SourceMap>) -> Result<String> {

First the generate_map method returns an External object, and then the JavaScript function holds the External object in closure:

module.exports.MagicString = class MagicString extends MagicStringNative {
  generateMap(options) {
    const sourcemap = super.generateMap({
      file: null,
      source: null,
      sourceRoot: null,
      includeContent: false,
    const toString = () => super.toSourcemapString(sourcemap)
    const toUrl = () => super.toSourcemapUrl(sourcemap)
    return {