Node - Search for dependancy recursively - node.js

I want to search recursively and find if I'm using a certain Node module. Is this possible?

If you mean not at runtime you can do:
npm ls <module name to search for>
For example:
$ npm ls async
/Users/justin/code/example
├─┬ aws-cdk#0.19.0
│ └─┬ archiver#2.1.1
│ └── async#2.1.4 deduped
├─┬ mongooster#6.0.2
│ └─┬ mongoose#5.3.5
│ └── async#2.6.1
└─┬ sqs-consumer#3.8.0
└── async#2.1.4
At runtime you can use require.resolve
> require.resolve('async')
'/Users/justin/code/example/node_modules/async/dist/async.js'
Or you can require the module then look in the cache to get details about the module, including its parent chain.
> require.cache[require.resolve('async')]
Module {
id: '/Users/justin/code/example/node_modules/async/dist/async.js',
exports: { ... },
parent: Module { ... },
filename: '/Users/justin/code/example/node_modules/async/dist/async.js',
loaded: true,
children: [],
paths: [ ... ]
}

Related

Specifying project path with react-scripts

I have a react project inside of a folder and I want react-scripts to target and compile from a folder. It looks like this
project
│ README.md
│ package.json
│
└───react
│ │ jsconfig.json
│ │
│ └───src
│ │
│ └───public
│
└───api
│ tsconfig.json
│
└───src
from the project/package.json I want to run react-scripts start and have it compile the /react folder. How can I do this?
I solved the issue with the use off react-app-rewired
See this stackoverflow post on details of how it was done.
const path = require('path');
module.exports = {
paths: function (paths, env) {
paths.appIndexJs = path.resolve(__dirname, 'react/src/index.js');
paths.appSrc = path.resolve(__dirname, 'react/src');
paths.appPublic = path.resolve(__dirname, 'react/public');
paths.appHtml = path.resolve(__dirname, 'react/public/index.html');
return paths;
}
}

how to ignore files within subdirectories using chokidar

I have this directory structure
├── components
│ ├── quarks
│ │ └── index.js
│ │ └── ...
│ ├── bosons
│ │ └── index.js
│ │ └── GridLayout.vue
│ │ └── ...
│ ├── atoms
│ │ └── ButtonStyle.vue
│ │ └── InputStyle.vue
│ │ └── index.js
│ │ └── ...
│ ├── .......
└─────
I'd like to ignore the index.js within each folder, but I'm not getting it, I've tried it in several ways
const path = require('path')
const chokidar = require('chokidar')
const ROOT_PATH = path.resolve('components')
const watcher = chokidar.watch(ROOT_PATH, {
ignored: ROOT_PATH + '/*/index.js', //does not work
ignoreInitial: true
})
already tried:
'./components/**/index.js',
'./components/*/index.js',
'components/*/index.js',
'components/**/index.js',
'ROOT_PATH + '/**/index.js'
Anyone have any idea how to make it work?
The chokidar documentation specifies that the ignored parameter is anymatch-compatiable so this could be completed in many ways.
Here is a regular expression solution...
Any index.js file, even in the root folder:
{
ignored: /(^|[\/\\])index\.js$/,
// ...
}
Only index.js file in a sub-folder:
{
ignored: /[\/\\]index\.js$/,
// ...
}
Also note in your example you use signoreInitial this is not an option, perhaps you meant ignoreInitial?
Alternatively with callback:
{
ignored: (path) => { return path.endsWith('\\index.js') || path.endsWith('/index.js'); },
// ...
}
Chokidar seems bugged, defective to ignore files on MacOS, that's the impression I have.
So before running my action, I'm checking to see if the file is the same one I want to ignore.
chokidar
.watch('components', { ignoreInitial: true })
.on('all', (event, filename) => {
filename !== 'index.js'
// action here
})
What worked for me on Mac was using **:
ignored: ['**/node_modules'],
So if the other options don't work because of bugs, go for this one:
ignored: ['**/index.js'],

Compile multiple projects

I have multiple themes with sass and a common folder with common options but the files should be compiled each theme css folder. It's a little complicated to explain. I want to share with you the scaffolding. I want to compile this with grunt but I don't know how to do
common_files
sass
common_file.scss
theme_foo
sass
file_one.scss
file_two.scss
css
style.scss
theme_bar
sass
file_one.scss
file_two.scss
css
style.scss
Gruntfile.js
I want to compile each theme with their own sass files and common files in their css folder.
Here is my Gruntfile.js
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
grunt.initConfig({
watch: {
sass: {
files: ['sass/{,*/}*.{scss,sass}'],
tasks: ['sass'],
},
},
sass: {
dist: {
options: {
style: 'expanded', // For compressed use 'compressed'
},
files: [{
expand: true,
cwd: 'sass',
src: ['*.{scss,sass}'],
dest: 'css',
ext: '.css',
},],
},
},
})
/* Load plugins */
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-csslint');
/* Tasks */
// Default task
grunt.registerTask('default', [, 'watch']);
};
Here is a complete solution to your problem :
Update your structure
├── project-dir
│ ├── common
│ │ ├── scss
│ │ │ ├── _button.scss
│ │ │ ├── _form.scss
│ │ │ ├── common.scss
│ ├── theme-foo
│ │ ├── scss
│ │ │ ├── _tag.scss
│ │ │ ├── _figure.scss
│ │ │ ├── theme-foo.scss
│ ├── theme-bar
│ │ ├── scss
│ │ │ ├── _tag.scss
│ │ │ ├── _figure.scss
│ │ │ ├── theme-bar.scss
Use main scss file of each folder (common, theme-foo, theme-bar) to import all its files. And for theme files, import alsocommon.scss` file.
E.g : common.scss file :
#import 'button.scss',
'form.scss';
E.g : theme-foo.scss file :
// import common style
#import 'common.scss';
// import theme style
#import 'tag.scss',
'form.scss';
Now you just have to compile theme-foo.scss and theme-bar.scss with grunt-contrib-sass.
E.g :
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
grunt.initConfig({
sass: {
dist: {
files: {
'project-dir/theme-foo/css/theme-foo.css': 'project-dir/**/theme-foo.scss',
'project-dir/theme-bar/css/theme-bar.css': 'project-dir/**/theme-bar.scss'
}
}
}
});
grunt.loadNpmTasks('grunt-sass');
};
Take a look sass-guidelines and specially 7-1 pattern to learn a lot of good practices.

How does npm draw the dependency tree?

npm does a nifty job of drawing a package's dependency hierarchy as a tree in the console:
$ npm ls
my-awesome-project#0.0.1
├── colors#0.6.0-1
├─┬ express#2.5.11
│ ├─┬ connect#1.9.2
│ │ └── formidable#1.0.11
│ ├── mime#1.2.4
│ ├── mkdirp#0.3.0
│ └── qs#0.4.2
└── uglify-js#1.2.6
How does npm do this?
npm uses the Unicode box drawing characters (U+2500-2800) to draw the pretty lines of the tree.
To draw a similar tree in your own application, the best route is probably to use the same module that npm itself uses – archy.
var archy = require('archy');
var s = archy({
label : 'beep',
nodes : [
'ity',
{
label : 'boop',
nodes : [
{
label : 'o_O',
nodes : [
{
label : 'oh',
nodes : [ 'hello', 'puny' ]
},
'human'
]
},
'party\ntime!'
]
}
]
});
console.log(s);
Outputs
beep
├── ity
└─┬ boop
├─┬ o_O
│ ├─┬ oh
│ │ ├── hello
│ │ └── puny
│ └── human
└── party
time!
For list your folders and files you can use tree-cli:
https://www.npmjs.com/package/tree-cli
Just install:
npm install -g tree-cli
And use inside your folder:
tree -L 2, -d
You could also use console2 which does almost the same thing as archy does, but gives you useful additional features like improved stack traces, object inspection and more:
Feature screenshot
Full disclosure: I'm the author of the repository

How to write a package.json file so that all dependencies are downloaded with "npm install"

I wrote a simple application using node. It depends on express, mongodb and mongoose (easy).
So, I created a file called package.json and put this in it:
{
"name": "booking-dojo",
"description": "Booking dojo app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongodb": "*",
"mongoose": "*"
}
}
I then ran npm install, expecting npm to install those modules and their dependencies.
The result was disappointing:
booking-dojo#0.0.1 /home/merc/Synced/Development/Bookings/app/server
├─┬ express#3.0.0rc3
│ ├── commander#0.6.1
│ ├─┬ connect#2.4.3
│ │ ├── bytes#0.1.0
│ │ ├── formidable#1.0.11
│ │ ├── pause#0.0.1
│ │ └── qs#0.4.2
│ ├── cookie#0.0.4
│ ├── crc#0.2.0
│ ├── debug#0.7.0
│ ├── fresh#0.1.0
│ ├── methods#0.0.1
│ ├── mkdirp#0.3.3
│ ├── range-parser#0.0.4
│ └─┬ send#0.0.3
│ └── mime#1.2.6
├─┬ mongodb#1.1.4
│ └── bson#0.1.1
└─┬ mongoose#3.0.3
├── hooks#0.2.1
└── ms#0.1.0
I am confused by this, as I know that express needs jade (and much more), and mongoose needs mongodb.
If I go into node_modules/jade and run npm install, the result from the main tree is very different:
booking-dojo#0.0.1 /home/merc/Synced/Development/Bookings/app/server
├─┬ express#3.0.0rc3
│ ├── commander#0.6.1
│ ├─┬ connect#2.4.3
│ │ ├── bytes#0.1.0
│ │ ├── formidable#1.0.11
│ │ ├── pause#0.0.1
│ │ └── qs#0.4.2
│ ├─┬ connect-redis#1.4.1
│ │ └─┬ redis#0.7.2
│ │ └── hiredis#0.1.14
│ ├── cookie#0.0.4
│ ├── crc#0.2.0
│ ├── debug#0.7.0
│ ├── ejs#0.8.2
│ ├── fresh#0.1.0
│ ├── github-flavored-markdown#1.0.1
│ ├─┬ hjs#0.0.4
│ │ └── hogan.js#2.0.0
│ ├─┬ jade#0.27.2
│ │ └── mkdirp#0.3.0
│ ├── methods#0.0.1
│ ├── mkdirp#0.3.3
│ ├─┬ mocha#1.4.0
│ │ ├── diff#1.0.2
│ │ ├── growl#1.5.1
│ │ └─┬ jade#0.26.3
│ │ └── mkdirp#0.3.0
│ ├── range-parser#0.0.4
│ ├─┬ send#0.0.3
│ │ └── mime#1.2.6
│ ├── should#1.1.0
│ ├─┬ stylus#0.29.0
│ │ └── cssom#0.2.5
│ └─┬ supertest#0.0.1
│ └─┬ superagent#0.5.0
│ ├── emitter-component#0.0.1
│ ├── formidable#1.0.9
│ ├── mime#1.2.5
│ └── qs#0.4.2
├─┬ mongodb#1.1.4
│ └── bson#0.1.1
└─┬ mongoose#3.0.3
├── hooks#0.2.1
└── ms#0.1.0
So, express has grown a lot. It looks like npm install is only loading some of the dependencies of the sub-modules.
Can somebody please shed some light on this? Why are some dependencies missing? Am I doing something wrong? (likely)
Thanks!
Merc.
You are confused about at least 2 points.
First, express does not depend on jade, as you can see by reading the node_modules/express/package.json file:
"dependencies": {
"connect": "2.4.2",
"commander": "0.6.1",
"range-parser": "0.0.4",
"mkdirp": "0.3.3",
"cookie": "0.0.4",
"crc": "0.2.0",
"fresh": "0.1.0",
"methods": "0.0.1",
"send": "0.0.3",
"debug": "*"
}
Express does, however, work with jade if it is available, as well as many other template engines. So to fix this list jade as a dependency in your package.json file and you'll be fine.
Second, npm only installs node packages, not third party things like mongodb. You need to install mongodb and any other dependencies that are not npm modules using other means (apt-get, yum, manual install, etc).
So npm DOES install dependencies recursively, but only npm modules.
The answer was provided by Brandon in a comment to another answer:
"Another thing to note is that if a package depends on a module that can be resolved further up in the dependency chain, it will. For example, since you have mongodb in your package.json, Mongoose doesn't need to install its own mongodb. – Brandon Tilley 2 days ago
Thank you Brandon! (And this is the answer...)
use this sample
{
"name": "app",
"version": "0.0.1",
"main":"test.js",
"author":"Test",
"description": "For test ",
"dependencies": {
"express": "latest",
"mongoose": "latest"
}
}

Resources