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

@flownet/lib-parse-imports-js

v0.3.6

Published

## Introduction

Downloads

125

Readme

@flownet/lib-parse-imports-js

Introduction

The @flownet/lib-parse-imports-js is a tool designed to analyze JavaScript and TypeScript files to determine their module import dependencies. This utility helps users understand which modules are imported, identifies unused imports, and distinguishes between local, npm, and Node.js built-in modules. It is useful for developers who want to manage and optimize their codebase by identifying unnecessary dependencies.

How It Works

The tool reads a specified JavaScript or TypeScript file and parses it to extract all import statements. It checks for both static imports and dynamic imports, as well as require() calls. The tool then categorizes each import as local, npm, or built-in Node.js module. It can also recursively analyze dependencies, providing a comprehensive view of the module's dependencies.

Key Features

  • Parses JavaScript and TypeScript files to list all module imports.
  • Differentiates between local, npm, and built-in Node.js modules.
  • Identifies unused or unreferenced imports in the code.
  • Supports recursive analysis of dependencies.
  • Outputs a detailed list of all, unreferenced, unnecessary, and required modules.

Conclusion

The @flownet/lib-parse-imports-js tool provides developers with valuable insights into their project's module dependencies. By identifying and categorizing imports, it helps improve code maintenance by addressing unused dependencies, thus making the codebase cleaner and potentially improving performance.

Developer Guide for "@flownet/lib-parse-imports-js"

Overview

The "@flownet/lib-parse-imports-js" library is designed to assist developers in analyzing JavaScript files to extract and analyze import paths. Its primary function is to identify all external modules, both local and npm-based, used within a JavaScript project. The library provides insights into imported modules, categorizing them into necessary and unnecessary modules based on their usage within the codebase.

Installation

You can install the "@flownet/lib-parse-imports-js" library using either npm or yarn. Here's how you can do it:

Using npm:

npm install @flownet/lib-parse-imports-js

Using yarn:

yarn add @flownet/lib-parse-imports-js

Usage

Below is a basic example of how to use the library to parse and analyze import statements in a JavaScript file. The main entry point to the library is the index function, which you can use as follows:

import parseImports from '@flownet/lib-parse-imports-js';

// Example usage
const analyzeImports = async () => {
  const result = await parseImports({
    file: '/path/to/your/file.js',  // specify the path to the JavaScript file
    recursive: true,                // whether to analyze imports recursively
  });

  console.log('All Modules:', result.all);
  console.log('Unreferenced:', result.unreferenced);
  console.log('Unnecessary:', result.unnecessary);
  console.log('Required:', result.required);
};

analyzeImports();

Examples

Here are some scenario-based examples that illustrate the core functionality of this library:

  1. Analyzing a Single File: To analyze a single file without following imports recursively, set the recursive option to false.

    parseImports({
      file: '/path/to/entry-file.js',
      recursive: false,
    }).then(result => {
      console.log(result);
    });
  2. Recursive Import Analysis: When you want to analyze your entire project, including all nested dependencies, set recursive to true.

    parseImports({
      file: '/path/to/project/index.js',
      recursive: true,
    }).then(result => {
      console.log(result);
    });
  3. Handling Node Built-in and Npm Modules: The library can distinguish between local, Node.js built-in, and npm modules, organizing them accordingly in the output.

    parseImports({
      file: './src/app.js',
      recursive: true,
    }).then(result => {
      console.log('Node Built-ins:', result.all.filter(mod => mod.type === 'node'));
      console.log('NPM Modules:', result.all.filter(mod => mod.type === 'npm'));
    });

Acknowledgement

This library leverages several open-source tools and libraries, notably Babel's parser and traverse modules, which are critical in parsing and walking through codebase to extract import statements efficiently. The contributors to these tools significantly aid in the development of functionalities provided by "@flownet/lib-parse-imports-js".

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  file:
    type: string
    description: The file path to be analyzed for module imports.
  recursive:
    type: boolean
    default: false
    description: Whether to recursively resolve all module imports in the specified file.
required:
  - file