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 🙏

© 2024 – Pkg Stats / Ryan Hefner

check-imports

v1.0.8

Published

Node.js tool that helps to control validity of imports and dependencies in a project

Downloads

599

Readme

check-imports npm version TypeScript

Checks import from declarations, import and require calls in your code and reports if:

  • Dependency is listed at package.json but never used at the folder relative to the package.json file.
  • A module is imported but dependency isn't listed at the closest package.json.

With --update option updates dependencies automatically (adds missing, removes unused).

Originally created for monorepos but can be used with any nested project structure with one or multiple package.json files.

TL;DR

npx check-imports

How it works

  1. The library gets all .js, .jsx, .ts, .mjs, .mts, .cjs files from CWD or directory relative to CWD that's defined with -d flag;
  2. Builds AST via ts-morph and retrieves import paths from import from, require(), import();
  3. Runs the following filtering and mapping:
    1. Ignore relative paths;
    2. Ignore built-in NodeJS modules such as node:path, path, node:fs, fs, etc;
    3. Retrieve module names (lodash/pick becomes lodash);
    4. Retrieve module names from scoped packages (@scope/module/foo/bar becomes @scope/module);
    5. Get rid of Webpack syntax (foo!bar!baz?quux=bat&xyzzy=plugh becomes baz);
  4. Finds a closest package.json file relative to the file with import;
  5. Compares its contents with the retrieved list of imports and if update option is set to true the script updates it.
    1. If ignore option is provided then "dependencies" aren't going to be updated with a given dependency or multiple dependencies;
    2. If a dependency already exists either at "dependencies", "optionalDependencies", "devDependencies" or "peerDependencies" then "dependencies" aren't going to be updated and a dependency version remains the same;
    3. If none of these two, a dependency of a latest version is retrieved from NPM registry and going to be added to "dependencies".

After an update you still need to run npm install manually.

Bonus: imports can be ignored directly in code via check-imports-ignore-line comment.

require('lodash'); // check-imports-ignore-line

CLI

The tool can be run via npx check-imports.

Options

  • -u, --update - update parent package.json files.
  • -e, --throw-error - exit process with code 1 in case if there are redundant or missing dependencies (good for CI).
  • -d, --directory-path <value> - a directory where JavaScript/TypeScript/JSX files are located.
  • --ignore-path <items> - a glob pattern (or coma-delimited patterns) to ignore processed files (--ignore-path "**/foo/*.js").
  • -n, --ignore-imports <items> - a comma-delimited list of dependencies that don't need to appear at "dependencies".

API

import checkImports from 'check-imports';

The API includes a bit wider set of options. It allows to map dependencies to check if a dependency needs to be ignored or get a wanted version.

const results = await checkImports(options);

Options

  • update = false - either update package.json files or not.
  • throwError = false - throw an error in case if there are redundant or missing dependencies
  • log = false - print CLI output
  • directoryPath = process.cwd() - a directory where JavaScript/TypeScript/JSX files are located.
  • ignorePath = [] - a glob pattern or an array of patterns to ignore processed files.
  • processManually = null - a function which is run agains every found import. You may want to define it in case if you want to ignore some dependencies or set a wanted version. It should return either of the following values:
    • true - process a dependency a regular way.
    • false - ignore a dependency.
    • An object with optional keys version and type. version field makes possible to forcibly define a dependency version. type field defines a key at package.json where a dependency needs to be stored ("optionalDependencies", "peerDependencies" or any custom). By default its value is "dependencies".
const results = await checkImports({
  directoryPath: path.resolve(__dirname, 'foo'),
  ignorePath: ['**/ignored.*'],
  processManually: (dependency) => {
    if (dependency === 'react') {
      // forcibly set version to 1.1.1 and add it to peerDependencies
      return {
        version: '1.1.1',
        type: 'peerDependencies',
      };
    }
    if (dependency === 'react-redux') {
      // add it to customDependencies
      return {
        type: 'customDependencies',
      };
    }
    if (dependency === 'moment') {
      // forcibly set version to ^999.999.999
      return {
        version: '^999.999.999',
      };
    }
    if (dependency === 'redux') {
      // ignore the dependency
      return false;
    }

    // else process regularly
    return true;
  },
  update: true,
});