Skip to content

4.1. Installation#

Adding the NPM package#

You can install Slang NPM package simply by running the following npm command:

bash
npm install "@nomicfoundation/slang"

Or if you are using yarn for package management:

bash
yarn add "@nomicfoundation/slang"

ESM vs CommonJS#

Slang is implemented in Rust, and compiled as a WASM component, which is exposed to TypeScript/JavaScript, and loaded asynchronously. If you are working with a modern ESM project, this will just work out of the box, with no additional configuration needed.

use-from-esm.mts
import assert from "node:assert";
import { Parser } from "@nomicfoundation/slang/parser";

test("top-level ESM import", () => {
  const parser = Parser.create("0.8.28");
  assert(parser);
});

But if you are working with a legacy CommonJS project, asynchronous imports are not supported. In that case, you can use the await import() syntax to load Slang:

use-from-commonjs.mts
import assert from "node:assert";

test("CommonJS await import", async () => {
  const { Parser } = await import("@nomicfoundation/slang/parser");

  const parser = Parser.create("0.8.28");
  assert(parser);
});