Check, if for a given file, an import is a node_modules import.
Using npm:
$ npm install is-node-modules-import
Using pnpm:
$ pnpm add is-node-modules-import
Using yarn:
$ yarn add is-node-modules-import
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
.
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
.
Using "prettier" as an example:
import <X> from "prettier"
import <X> from "prettier/plugins"
import <X> from "./node_modules/prettier"
import <X> from "home/user/.../prettier"
import <X> from "home/user/.../node_modules/prettier"
If you provide an import path with a file from node_modules
which doesn't exist, the function will return false.