What is find-node-modules?
The find-node-modules npm package is a utility that helps you locate the node_modules directories in your project. This can be particularly useful in monorepos or projects with complex directory structures where node_modules might not be in the root directory.
What are find-node-modules's main functionalities?
Find node_modules in the current directory
This feature allows you to find all node_modules directories starting from the current directory. The code sample demonstrates how to use the package to get an array of paths to node_modules directories.
const findNodeModules = require('find-node-modules');
const paths = findNodeModules();
console.log(paths);
Find node_modules in a specific directory
This feature allows you to specify a starting directory to search for node_modules directories. The code sample shows how to find node_modules directories starting from a given path.
const findNodeModules = require('find-node-modules');
const paths = findNodeModules('/path/to/start');
console.log(paths);
Find node_modules with custom options
This feature allows you to customize the search with options such as the starting directory (cwd) and whether to return relative paths. The code sample demonstrates how to use these options.
const findNodeModules = require('find-node-modules');
const paths = findNodeModules({ cwd: '/path/to/start', relative: false });
console.log(paths);
Other packages similar to find-node-modules
find-up
The find-up package is used to find a file or directory by walking up parent directories. It is more general-purpose compared to find-node-modules, as it can locate any file or directory, not just node_modules.
pkg-dir
The pkg-dir package finds the root directory of a Node.js project by looking for a package.json file. While it doesn't specifically find node_modules directories, it helps in locating the project root, which can be useful in conjunction with other tools.
resolve
The resolve package is used to resolve module paths as Node.js does. It can be used to find the location of a module, including its node_modules directory. It offers more functionality related to module resolution compared to find-node-modules.
find-node-modules
This is a little node module to find the path of every parent node_modules
directory. It's useful for things like Sass, where you can't specify the exact
path to individual modules (in which case findup-sync would be sufficient),
and you can't just give an array of parent node_modules which might exist,
because it will error if they don't.
In most cases you're trying to find node_modules directories, findup-sync
should be sufficient. This library is specifically for if you want an array
containing all the parent node_modules paths. If you loop through the output
of this library, you should be using findup-sync instead.
Install
$ npm install --save find-node-modules
Usage
var findNodeModules = require('find-node-modules');
findNodeModules();
findNodeModules({ cwd: './someDir' });
findNodeModules('./someDir');
findNodeModules({ cwd: './someDir', relative: false });
License
This is released under the MIT license.