Checksum Utility for Node.js
The `checksum` package by `dshaw` is a minimalistic utility for Node.js environments, designed to compute cryptographic hashes for strings and local files. It supports algorithms such as SHA1 (which is the default) and MD5, alongside others provided by Node.js's built-in `crypto` module. The package's current and only stable version, 1.0.0, was last published approximately five years ago (as of April 2026), indicating it is no longer under active maintenance or development. This makes it suitable primarily for legacy CommonJS Node.js projects. While it offers a straightforward API for both direct string hashing and asynchronous file hashing, as well as a command-line interface, developers should be aware of its unmaintained status and the use of older, less secure default hashing algorithms like SHA1 and MD5 for modern integrity verification needs. More contemporary Node.js projects often leverage the native `crypto` module directly or use newer, actively maintained third-party libraries that offer ESM support and stronger defaults.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('checksum')` in an ES Module (`.mjs` file or project with `"type": "module"` in `package.json`) context.fixIf staying with ESM, use `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const checksum = require('checksum');`. Otherwise, convert your project to CommonJS by setting `"type": "commonjs"` in `package.json` or renaming files to `.cjs`. -
npm ERR! code EINTEGRITY
cause This error typically occurs during `npm install` when a package's checksum in `package-lock.json` doesn't match the downloaded package. This can happen with older packages due to `npm` changing its default integrity algorithm from SHA1 to SHA512, or due to cache corruption.fixFirst, try `npm cache clean --force`. If the problem persists, delete `node_modules/` and `package-lock.json`, then run `npm install` again. If the issue is persistent and related to SHA1, consider avoiding this outdated package.
Warnings
- breaking The `checksum` package has not been updated in approximately five years and is considered abandoned. It may contain unaddressed bugs or security vulnerabilities and is not recommended for new projects, especially those with strict security requirements.
- gotcha The default hashing algorithm for both string and file checksums is SHA1. SHA1 is cryptographically weak and has known collision vulnerabilities, making it unsuitable for integrity verification in many modern applications.
- gotcha This package is designed for CommonJS (CJS) environments and uses `require()` syntax. It does not natively support ES Modules (ESM) `import` syntax, which is standard in modern Node.js projects with `"type": "module"` in `package.json`.
- gotcha The `checksum` package does not provide TypeScript type definitions. Developers using TypeScript will lack type safety and autocompletion without manually creating declaration files or relying on community-contributed `@types/checksum` packages (if available and up-to-date).
Install
-
npm install checksum -
yarn add checksum -
pnpm add checksum
Imports
- checksum
import checksum from 'checksum'
const checksum = require('checksum') - checksum.file
import { file } from 'checksum'; file('path/to/file.txt', ...);const checksum = require('checksum'); checksum.file('path/to/file.txt', (err, sum) => { /* ... */ });
Quickstart
const checksum = require('checksum');
const fs = require('fs');
// Calculate checksum for a string
const stringToHash = 'Hello, world!';
const stringChecksum = checksum(stringToHash);
console.log(`Checksum for '${stringToHash}': ${stringChecksum}`);
// Calculate checksum for a file
const filePath = 'example.txt';
fs.writeFileSync(filePath, 'This is a test file for checksum calculation.');
checksum.file(filePath, { algorithm: 'sha256' }, (err, sum) => {
if (err) {
console.error('Error calculating file checksum:', err);
return;
}
console.log(`Checksum (SHA256) for '${filePath}': ${sum}`);
fs.unlinkSync(filePath); // Clean up the test file
});
// Using the CLI tool (requires global install: npm install -g checksum)
// To run in terminal: echo -n 'dshaw' | checksum
// Or: checksum ./example.txt