Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lexoyo/node_modules-path
Get the path of the `node_modules` folder in your scripts or CLI or package.json
https://github.com/lexoyo/node_modules-path
node-module nodejs npm utility
Last synced: 8 days ago
JSON representation
Get the path of the `node_modules` folder in your scripts or CLI or package.json
- Host: GitHub
- URL: https://github.com/lexoyo/node_modules-path
- Owner: lexoyo
- Created: 2017-06-01T12:45:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-02T18:43:00.000Z (about 1 month ago)
- Last Synced: 2024-10-13T23:13:18.399Z (23 days ago)
- Topics: node-module, nodejs, npm, utility
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/node_modules-path
- Size: 18.6 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# node_modules Path
Get the path of the `node_modules` folder in your scripts or CLI or `package.json`. This is useful when you are building a library that can either be used as an npm dependency or directly, [see this question on SO](https://stackoverflow.com/questions/44279838/copy-assets-from-npm).
You can add a parameter in order to look for a specific module. This is useful when npm creates [multiple node_modules folder for conflicts reasons](https://docs.npmjs.com/files/folders#cycles-conflicts-and-folder-parsimony).
Installation
```
$ npm install node_modules-path --save
```Test
```
$ npm test
```This project has been battle tested on [Silex website builder](http://www.silex.me)
## Access node_modules in package.json
Use it in your `packge.json` like this:
```json
"name": "my-super-project",
"scripts": {
"build": "cp -R `node_modules`/font-awesome/fonts/* dist/fonts/", <=== this will be executed as cp -R /path-to-your-node_modules/or-the-node_modules-of-a-parent/font-awesome/fonts/* dist/fonts/
```With a specific module name as a param:
```json
"scripts": {
"test": "echo `node_modules` can differ from `node_modules module1`", <=== this will be executed as cp -R /path-to-your-node_modules/or-the-node_modules-of-a-parent/font-awesome/fonts/* dist/fonts/```
## node_modules folder path from another nodejs script
```
const node_modules = require('node_modules-path');
console.log('node module path for this project:', node_modules());
```This is especially useful to serve fonts in an express app
```js
app.use('/fonts', express.static(Path.resolve(node_modules(), 'font-awesome/fonts/')));
// node_modules() will return the path to your node_modules or to the node_modules of a parent
```With a specific module name as a param, which is safer:
```js
app.use('/fonts', express.static(Path.resolve(node_modules('font-awesome'), 'font-awesome/fonts/')));
// node_modules('font-awesome') will return the path to your node_modules or to the node_modules of a parent
```## node_modules folder path for sass includes
In your `package.json` script to compile sass:
```json
{
"build:sass": "sass --load-path `node_modules normalize.css` src/css/app.scss dist/css/app.css"
}```