Skip to content

Check, if for a given file, an import is a node_modules import

License

Notifications You must be signed in to change notification settings

Qqkyu/is-node-modules-import

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

is-node-modules-import

npm version npm downloads

Check, if for a given file, an import is a node_modules import.

Installing

Using npm:

$ npm install is-node-modules-import

Using pnpm:

$ pnpm add is-node-modules-import

Using yarn:

$ yarn add is-node-modules-import

Usage

Import the function using require:

const isNodeModulesImport = require('is-node-modules-import');

isNodeModulesImport expects a file path as the first argument and an import path as the second one. For example:

isNodeModulesImport('index.js', 'prettier');

You can check more examples in index.test.js.

Behavior

This package checks, if an import is node_modules import on any level, not only project-level.

This means, that if you have a project with the following structure:

- node_modules
    - react
    - eslint
- index.js

Then running isNodeModulesImport on the index.js file will not only check node_modules on this level (with react and eslint). It will check also any node_modules directories further down the file tree.

So if you had a node_modules directory with prettier installed in the root of your system, then the function will return true.

Tested cases

Using "prettier" as an example:

Simple node_modules imports

  • import <X> from "prettier"
  • import <X> from "prettier/plugins"

Relative node_modules imports

  • import <X> from "./node_modules/prettier"

Absolute node_modules imports

  • import <X> from "home/user/.../prettier"
  • import <X> from "home/user/.../node_modules/prettier"

Non-existent node_modules

If you provide an import path with a file from node_modules which doesn't exist, the function will return false.

About

Check, if for a given file, an import is a node_modules import

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published