npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

chemical-formula

v5.1.0

Published

Parse a chemical formula to get a count of each element in a compound

Readme

chemical-formula

Parse a chemical formula to get a count of each element in a compound.

Example

const chemicalFormula = require("chemical-formula");

// Simple formulas
chemicalFormula("H2O");
// => {H: 2, O: 1}

chemicalFormula("HOCH2CH2OH");
// => {H: 6, O: 2, C: 2}

// Parentheses with multipliers
chemicalFormula("Ca(OH)2");
// => {Ca: 1, O: 2, H: 2}

chemicalFormula("Al2(SO4)3");
// => {Al: 2, S: 3, O: 12}

chemicalFormula("Mg(NO3)2");
// => {Mg: 1, N: 2, O: 6}

// Nested parentheses
chemicalFormula("Mg3(Fe(CN)6)2");
// => {Mg: 3, Fe: 2, C: 12, N: 12}

// Hydrates with dot notation
chemicalFormula('CuSO4·5H2O');
// => {Cu: 1, S: 1, O: 9, H: 10}

chemicalFormula('CaCl2·2H2O');
// => {Ca: 1, Cl: 2, O: 2, H: 4}

// Period notation also supported
chemicalFormula('MgSO4.7H2O');
// => {Mg: 1, S: 1, O: 11, H: 14}

// Complex hydrates with parentheses
chemicalFormula('Al2(SO4)3·18H2O');
// => {Al: 2, S: 3, O: 30, H: 36}

// Multiple hydrates (sequential, not cascading)
chemicalFormula('CuSO4·5H2O·2NH3');
// => {Cu: 1, S: 1, O: 9, H: 16, N: 2}
// Note: Each dot resets multiplier context (5H2O + 2NH3, not 5(H2O + 2NH3))

Installation

$ npm install chemical-formula

Hydrate Notation

Use middle dot (·, U+00B7) for hydrate formulas like CuSO4·5H2O. Period (.) is accepted for convenience (e.g., MgSO4.7H2O), but · is preferred.

Requirements:

  • Multipliers must be positive integers
  • Decimals and fractions are not yet supported
  • Dots must have content on both sides (no leading/trailing/consecutive dots)
  • Very large integers are supported (tested up to 123456789)

Note: Future support for decimal coefficients may affect period interpretation.

API

const chemicalFormula = require("chemical-formula");

chemicalFormula(formula)

Parse String formula and return an Object whose keys are element symbols and values are the quantities of each element in formula.