A self-contained TypeScript wrapper around the official IUPAC InChI C library compiled to WebAssembly. Convert MDL Molfiles to InChI/InChIKey and back, in Node and in the browser, without any external file or fetch — the WASM binary is gzip-compressed and base64-embedded inside the package.
The published library lives in
packages/inchi-js/ and is released to npm as
inchi-js.
npm install inchi-jsimport {
inchiFromMolfile,
inchikeyFromInchi,
molfileFromInchi,
} from 'inchi-js';
const molfile = `\
Mrv2014 01010100002D
3 2 0 0 0 0 999 V2000
0.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
1.0000 0.0000 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0
2.0000 0.0000 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0
1 2 1 0 0 0 0
2 3 1 0 0 0 0
M END
`;
const { inchi, auxinfo } = await inchiFromMolfile(molfile);
// → 'InChI=1S/C2H6O/c1-2-3/h3H,2H2,1H3'
const { inchikey } = await inchikeyFromInchi(inchi);
// → 'LFQSCWFLJHTTHZ-UHFFFAOYSA-N'
const { molfile: regenerated } = await molfileFromInchi(inchi);All four conversion functions — inchiFromMolfile,
inchikeyFromInchi, molfileFromInchi, and molfileFromAuxinfo — are
async: the WASM module is instantiated lazily on first call and
cached forever after. See
packages/inchi-js/README.md for the
full API reference, option strings, and details on how the WASM binary
is shipped.
The official IUPAC InChI C library lives as a git submodule under
vendor/inchi/, pinned to a known commit of
IUPAC-InChI/InChI. Clone with:
git clone --recurse-submodules https://github.com/cheminfo/inchi.git
# or if you already cloned:
git submodule update --init --recursivenpm install
npm test # type-check, lint, and run the test suiteThe library source is in packages/inchi-js/src/.
npm run build-lib compiles it to packages/inchi-js/lib.
The library ships with a pre-built, base64-embedded WASM module at
packages/inchi-js/src/wasm/data.ts.
To rebuild it from the InChI C source you need
Emscripten (emcc) and cmake ≥ 3.15:
npm run build-wasmThis:
- Configures and compiles
vendor/inchi/INCHI-1-SRC/INCHI_BASEandvendor/inchi/INCHI-1-SRC/INCHI_API/libinchiplus the IUPACinchi_web.cJSON wrapper withemccinto a.wasm+ JS glue. - Gzips and base64-encodes the resulting
.wasmbinary intopackages/inchi-js/src/wasm/data.ts(next to the auto-generated JS glue atpackages/inchi-js/src/wasm/glue.ts). - Generates a TypeScript module that decodes + decompresses the binary at load time and instantiates a WebAssembly instance.
The pre-built artifact is checked in so consumers of the npm package never need a C toolchain.
| Command | Effect |
|---|---|
npm run build-lib |
Compile the library to packages/inchi-js/lib. |
npm run build-wasm |
Rebuild the WASM module from vendor/inchi/ (requires emscripten + cmake). |
npm test |
vitest run --coverage + type-check + eslint + prettier. |
npm run test-only |
Tests only, skip lint/types. |
This library is a thin TypeScript wrapper around the official IUPAC
InChI C software (MIT-licensed). The inchi_web.c JSON-emitting
wrapper layer and the emscripten build approach are adapted from
IUPAC-InChI/InChI-Web-Demo
(also MIT). All trademarks and the InChI algorithm itself belong to
IUPAC.