Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Access underscore.js from client after using npm in node.js

Access underscore.js from client after using npm in node.js

Problem

In Node.js, I installed the underscore.js module. Tried to include it in the layout like this:

script(src='/underscore/underscore-min.js')
script(src='/socket.io/socket.io.js')

But socket.io is the only one that was found. In node_modules/socket.io, there is no socket.io.js file, but in node_modules/underscore/, there is a udnerscore-min.js. How exactly am I pulling down socket.io but not underscore.js? Does socket.io run some code and create the socket.io/ directory and put the file there? If so, what method is underscore.js using?

Problem courtesy of: Shawn Mclean

Solution

I assume your using the express framework with something like:

var app = express.createServer(),
          io = io.listen(app);

If that is the case then Socket.io will automatically create a route for /socket.io/socket.io.js. If you want to server underscore-min.js you could try symlinking it to a static directory (which is a bit hacky...).

I guess using something like Stitch or minifying all your external libraries to one file would be a better solution and it would also reduce the amount of HTTP requests...

EDIT: I just discovered visionmedia/asset which is really amazing! It still doesn't provide access to your modules inside node_modules, but it makes this whole javascript asset management hell not only baerable, but also fun.

You've got an app using express.js? Let's say you want to provider the brwowser with Jquery and underscore. All You have to now is:

$ asset jquery underscore -c -o public/js

         install : [email protected]
         install : [email protected]
        download : [email protected]
        complete : [email protected] public/js/jquery.min.js
        download : [email protected]
        complete : [email protected] public/js/underscore.min.js

And that's it! :-)

Solution courtesy of: Sebastian Stumpf

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Access underscore.js from client after using npm in node.js

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×