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
Related
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: [ ... ]
}
I am attempting to add a GUI to a small project of mine using Conrod. I have managed to work my way down to 3 compilation errors:
error[E0433]: failed to resolve. Could not find `glutin` in `glium`
--> src/support/mod.rs:88:53
|
88 | pub fn next(&mut self, events_loop: &mut glium::glutin::EventsLoop) -> Vec<glium::glutin::Event> {
| ^^^^^^ Could not find `glutin` in `glium`
error[E0433]: failed to resolve. Could not find `glutin` in `glium`
--> src/support/mod.rs:88:87
|
88 | pub fn next(&mut self, events_loop: &mut glium::glutin::EventsLoop) -> Vec<glium::glutin::Event> {
| ^^^^^^ Could not find `glutin` in `glium`
error[E0433]: failed to resolve. Could not find `glutin` in `glium`
--> src/support/mod.rs:106:24
|
106 | glium::glutin::ControlFlow::Break
| ^^^^^^ Could not find `glutin` in `glium`
I've studied the examples that ship with Conrod (particularly the text_edit.rs example) and have successfully compiled and run them. As far as I can tell, they use the same techniques (as my code is directly inspired by their examples), yet does not suffer from the unresolved imports of glutin.
Furthermore, I cannot seem to find any reference to glutin in the project directory itself:
$> pwd
~/dev/conrod/src
$> tree.
.
├── backend
│ ├── gfx.rs
│ ├── glium.rs
│ ├── mod.rs
│ ├── piston
│ │ ├── draw.rs
│ │ ├── event.rs
│ │ └── mod.rs
│ └── winit.rs
├── border.rs
├── color.rs
├── cursor.rs
├── event.rs
├── graph
│ ├── algo.rs
│ ├── depth_order.rs
│ └── mod.rs
├── guide
│ ├── chapter_1.rs
│ ├── chapter_2.rs
│ └── mod.rs
├── image.rs
├── input
│ ├── global.rs
│ ├── mod.rs
│ ├── state.rs
│ └── widget.rs
├── label.rs
├── lib.rs
├── position
│ ├── matrix.rs
│ ├── mod.rs
│ ├── range.rs
│ └── rect.rs
├── render.rs
├── tests
│ ├── global_input.rs
│ ├── mod.rs
│ ├── ui.rs
│ └── widget_input.rs
├── text.rs
├── theme.rs
├── ui.rs
├── utils.rs
└── widget
├── bordered_rectangle.rs
├── builder.rs
├── button.rs
├── canvas.rs
├── collapsible_area.rs
├── drop_down_list.rs
├── envelope_editor.rs
├── file_navigator
│ ├── directory_view.rs
│ └── mod.rs
├── graph
│ ├── mod.rs
│ └── node.rs
├── grid.rs
├── id.rs
├── list.rs
├── list_select.rs
├── matrix.rs
├── mod.rs
├── number_dialer.rs
├── plot_path.rs
├── primitive
│ ├── image.rs
│ ├── line.rs
│ ├── mod.rs
│ ├── point_path.rs
│ ├── shape
│ │ ├── circle.rs
│ │ ├── mod.rs
│ │ ├── oval.rs
│ │ ├── polygon.rs
│ │ ├── rectangle.rs
│ │ └── triangles.rs
│ └── text.rs
├── range_slider.rs
├── rounded_rectangle.rs
├── scrollbar.rs
├── scroll.rs
├── slider.rs
├── tabs.rs
├── text_box.rs
├── text_edit.rs
├── title_bar.rs
├── toggle.rs
└── xy_pad.rs
For reference, my Cargo.toml also includes glutin as a dependency:
[features]
default = ["winit", "glium"]
winit = ["conrod/winit"]
glium = ["conrod/glium"]
[dependencies]
conrod = "^0.57"
find_folder = "*"
glutin = "*"
I believe this is a misconception about the module structure of conrod and glium.
The conrod crate has a number of backend modules, containing utility functions for each of the different backends. conrod::backend::glium is this module for glium, and it contains structures and things useful for using conrod with glium.
In your case, however, I think you mistook this module for glium itself.
glium is a separate crate from conrod, and you'll need to depend on it much like you depend on glutin. glium does indeed have a glium::conrod property, so if you do pull it in with extern crate glium; rather than using conrod::backend::glium, it should "just work"!
You'll need to add some line glium = 0.x in your Cargo.toml as well, but that should be trivial.
This is my configure file:
The layout.jade does not seem to be working. But the jade is working. I used Chrome to check, and am sure that the layout HTML is not loaded into the page.
module.exports = function(app, express, mongoose){
var config=this
app.configure(function (){
app.set('views',__dirname+'/views')
app.set('view engine','jade')
app.set('view options', {layout:true})
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(express.cookieParser())
app.use(express.session({secret: 'topsecret',store: new express.session.MemoryStore}))
app.use(express.static(app.path.join(app.application_root,"public")))
app.use(express.errorHandler({dumpExceptions:true,showStack:true}))
app.use(express.bodyParser({keepExtensions: true, uploadDir:"./public/uploads"}))
app.use(app.router)
})
/*DB part:*/
app.mongoose.connect('mongodb://localhost/dio_database')
return config
}
The render command:
app.get('/items/:id',function(req,res){
models.ItemModel.findOne({_id:req.params.id}).exec(function(err,item){
if (!err){
res.render('item.jade',item)
} else
return console.log(err)
})
})
My layout.jade, quite simple:
!!!
doctype 5
html
head
title "Dio"
link(rel='icon', href='favicon.ico', type='image/x-icon')
link(rel='shortcut', href='favicon.ico', type='image/x-icon')
link(rel="shortcut", href="favicon.ico", type="image/vnd.microsoft.icon")
link(rel="icon", href="favicon.ico", type="image/vnd.microsoft.icon")
script(src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js")
script(src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js")
script(src="./javascripts/underscore-min.js")
script(src="./javascripts/backbone-min.js")
link(rel='stylesheet', href='./css/main.css', type="text/css", media="screen")
body
div#topbar Dio--where shitty thing happens
div#main!= body
footer
p
| Node.js MVC template by XXX
And the following is my npm list:
├─┬ bcrypt#0.7.3
│ └── bindings#1.0.0
├─┬ express#3.0.3
│ ├── commander#0.6.1
│ ├─┬ connect#2.7.0
│ │ ├── bytes#0.1.0
│ │ ├── formidable#1.0.11
│ │ ├── pause#0.0.1
│ │ └── qs#0.5.1
│ ├── cookie#0.0.5
│ ├── cookie-signature#0.0.1
│ ├── 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.1.0
│ └── mime#1.2.6
├── fs#0.0.0
├── imagemagick#0.1.3
├─┬ jade#0.27.7
│ ├── coffee-script#1.4.0
│ ├── commander#0.6.1
│ └── mkdirp#0.3.4
├─┬ mongodb#1.2.2
│ └── bson#0.1.5
├─┬ mongoose#3.4.0
│ ├── hooks#0.2.1
│ ├─┬ mongodb#1.1.11
│ │ └── bson#0.1.5
│ ├── ms#0.1.0
│ └── sliced#0.0.3
├─┬ node-static#0.6.5 extraneous
│ ├── colors#0.6.0-1
│ └─┬ optimist#0.3.5
│ └── wordwrap#0.0.2
└── path#0.4.9
Actually the reason for such problem is quite simple:
Express 3 no longer supports layout..But do not be sad. Actually Express 3 begins to adopt a more natural and easier way, which is called block/extends.
The basic usage should be like this:
// In layout file: layout.jade
html
head
title XXX
block scripts
body
block content
block footer
// in a extended file, for example index.jade:
extends layout
block scripts
//write javascript part
block content
// write content
block footer
// write the footer
It actually becomes easier and more flexible. Glad to get it finally. But it took me more than 2 hours.
I am just wondering why so few people mentioned this change more clearly and openly. Hope this post can help some people like me.
I'm new to NodeJS and trying to build an app over Express3.0, included passport local strategy for authentication purpose. But the following exception(with respect to req.flash) blocks my progress.
Exception occurs in the following line.
res.render('login', { user: req.user, message: req.flash('error') });
Express
500 TypeError: Object # has no method 'flash'
at /Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/app.js:115:54
at callbacks (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/lib/router/index.js:162:37)
at param (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/lib/router/index.js:136:11)
at pass (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/lib/router/index.js:143:5)
at Router._dispatch (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/lib/router/index.js:171:5)
at Object.router (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/lib/router/index.js:33:10)
at next (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at store.get.next (/Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/node_modules/connect/lib/middleware/session.js:310:9)
at /Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/node_modules/connect/lib/middleware/session.js:333:9
at /Users/vivekanandan/Source/Git/ExpressApp/CosmicEnergyCoupled/node_modules/express/node_modules/connect/lib/middleware/session/memory.js:52:9
I have installed connect-flash to recover the deprecated req.flash method as advised by the author(passport-local-strategy). Please find the npm packages installed in the app.
├── connect-flash#0.1.0
├── ejs#0.8.3
├── ejs-locals#0.2.5
├─┬ express#3.0.0
│ ├── commander#0.6.1
│ ├─┬ connect#2.6.0
│ │ ├── bytes#0.1.0
│ │ ├── formidable#1.0.11
│ │ ├── pause#0.0.1
│ │ ├── qs#0.5.1
│ │ └─┬ send#0.0.4
│ │ └── mime#1.2.6
│ ├── 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.1.0
│ └── mime#1.2.6
├─┬ passport#0.1.12
│ └── pkginfo#0.2.3
├─┬ passport-local#0.1.6
│ ├── passport#0.1.12
│ └── pkginfo#0.2.3
├─┬ socket.io#0.9.10
│ ├── policyfile#0.0.4
│ ├── redis#0.7.2
│ └─┬ socket.io-client#0.9.10
│ ├─┬ active-x-obfuscator#0.0.1
│ │ └── zeparser#0.0.5
│ ├── uglify-js#1.2.5
│ ├─┬ ws#0.4.22
│ │ ├── commander#0.6.1
│ │ ├── options#0.0.3
│ │ └── tinycolor#0.0.1
│ └── xmlhttprequest#1.4.2
└─┬ stylus#0.30.1
├── cssom#0.2.5
├── debug#0.7.0
└── mkdirp#0.3.4
Try adding this to your main app.configure method
app.use(flash());
npm install connect-flash --save
var flash = require('connect-flash')
app.use(flash());
You're using it wrong I think. You just have to call req.flash() with type and message before res.redirect
req.flash('info', 'Welcome to the site, a welcome email has been sent to you.');
res.redirect('/');
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"
}
}