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

precinct

v13.0.1

Published

Unleash the detectives

Readme

precinct

CI npm version npm downloads

Unleash the detectives

Uses the appropriate detective to find the dependencies of a file or its AST.

Supports:

  • JavaScript modules: AMD, CommonJS, ES6
  • TypeScript
  • CSS preprocessors: Sass, Scss, Less, Stylus
  • CSS (PostCSS)
  • Vue

Install

npm install precinct

Quick start

// ESM
import fs from 'node:fs';
import precinct from 'precinct';
// CommonJS
const fs = require('node:fs');
const { default: precinct } = require('precinct');

const content = fs.readFileSync('myFile.js', 'utf8');

// From source content or an AST
const deps = precinct(content);

// Or directly from a file path
const deps2 = precinct.paperwork('styles.scss');

API

precinct(content, options?)

Returns an array of dependency strings discovered in content. content may be a source string or an already-parsed AST.

| Option | Type | Default | Notes | |---|---|---|---| | type | string | inferred from source | Forces a specific detective. See Supported types. | | walker | object | - | Passed through to the underlying node-source-walk instance - e.g. { allowImportExportEverywhere: true }, or { parser: myCustomParser } to swap in a parser with a .parse(src, opts) method. | | amd.skipLazyLoaded | boolean | false | Omit lazy-loaded (inner-require) dependencies in AMD files. | | es6.mixedImports | boolean | false | Return both ES6 and CommonJS imports from a file that mixes the two. Works for any format that contains an ES6 import. | | css.url | boolean | false | Include url() references (images, fonts, etc.) in CSS output. | | [type] | object | - | Any other key matching a module type is forwarded to that detective as its options bag. |

Side channel: precinct.ast holds the last AST produced (or null when parsing failed).

Example

precinct(content, {
  type: 'amd',
  amd: {
    skipLazyLoaded: true
  }
});

precinct(content, {
  walker: {
    allowImportExportEverywhere: true
  }
});

// Non-JS content
precinct(scssSource, { type: 'scss' });
precinct(stylusSource, { type: 'stylus' });

precinct.paperwork(filename, options?)

Reads the file at filename and returns an array of its dependencies. The module type is inferred from the file extension (see below). Accepts every option precinct() does, plus the two below.

| Option | Type | Default | Notes | |---|---|---|---| | includeCore | boolean | true | Set to false to strip Node.js core modules (fs, path, node:fs, ...) from the result. | | fileSystem | { readFileSync(path, encoding): string } | node:fs | An alternative fs implementation used to read filename. Only readFileSync(path, 'utf8') is required. | | walker, amd, es6, css, [type] | - | - | Same as precinct() - all detective options are forwarded. |

Example

// ESM
import { paperwork } from 'precinct';
// CommonJS
const { paperwork } = require('precinct');

const deps = paperwork('myFile.js');
const deps2 = paperwork('styles.scss');
const deps3 = paperwork('app.ts', { includeCore: false });

Supported types

Accepted values for the type option:

| Value | Detective | |---|---| | amd | detective-amd | | cjs, commonjs | detective-cjs | | css | detective-postcss | | es6, esm, mjs | detective-es6 | | less | @dependents/detective-less | | sass | detective-sass | | scss | detective-scss | | stylus | detective-stylus | | ts | detective-typescript | | tsx | detective-typescript (tsx variant) | | vue | detective-vue2 |

paperwork() infers the type from the filename extension; .styl maps to stylus and .cjs maps to commonjs. Any other extension becomes the type with the leading dot stripped. .js and .jsx are sniffed at the source level rather than by extension.

CLI

npm install -g precinct
precinct [options] <filename>

| Flag | Description | |---|---| | -t, --type <type> | Force a module type (see Supported types). | | --es6-mixed-imports | Collect both ES6 and CommonJS imports in the same file. | | -V, --version | Print version. | | -h, --help | Print help. |

License

MIT