Docs
Concepts
Exports

Exports

💡

Unlike defining modules in Node.js, we don't need to explicitly register exports like module.exports.xxx = xxx.

The #[napi] macro will automatically generate module registering code for you. This auto registering idea was inspired by node-bindgen.

Function

EXporting a function is incredibly simple. Just decorate a normal rust function with #[napi]:

#[napi]
fn sum(a: u32, b: u32) -> u32 {
	a + b
}

Const

#[napi]
pub const DEFAULT_COST: u32 = 12;
export const DEFAULT_COST: number

Class

See class section for more details.

#[napi(constructor)]
struct Animal {
  pub name: String,
  pub kind: u32,
}

#[napi]
impl Animal {
  #[napi]
  pub fn change_name(&mut self, new_name: String) {
    self.name = new_name;
  }
}

Enum

See enum section for more details.

#[napi]
pub enum Kind {
  Dog,
  Cat,
  Duck,
}