How do I properly import jQuery plugins to Node.JS?

Go To StackoverFlow.com

8

Background

I am new to Node.JS but very experienced with JavaScript and jQuery. I have had no problem installing jQuery via npm install jquery, however, referencing plugins within the code is another challenge.

I have reviewed this similar StackOverflow question, and the solution appears to work but it seems to me that instantiating a "fake" browser window and injecting your jQuery plugin-based functions each time you need the plugin is possibly not the most efficient approach.

The specific plugin that is failing for me linq.js (yes, I am aware that js linq is available via npm but it is not the same as linq.js!).

NOTE: The plugin to which I am referring does not rely on any DOM elements; in my case, it simply runs JSON objects through various data functions. This is why I don't think I need to instantiate a window object.

Question

How do I properly import and use jQuery plugins in a Node.JS application?

2012-04-05 15:45
by Matt Cashatt
Thanks for sharing linq.js, looks awesome. Since no one did it, I took the time to package it for node and publish it to npm (the changes were trivial). https://github.com/mihaifm/linq It currently works as a standalone library and not as a plugin, so I don't know how to answer your question : - mihai 2012-04-07 12:23
jQuery is not something that should run on node. You don't do this. Ever. Just because you can take 10 hits of acid and run naked through the streets doesn't mean you should; just because you can run JavaScript on the server doesn't mean you should try and put jQuery there - Incognito 2012-04-08 23:00
@Incognito--yes, but why??? You are obviously good at making a cliche and chic statement about how not to use a technology but your comment isn't helpful. Tell us why you say this and I might come over to your way of thinking. I will grant you that jQuery isn't meant for node, but it DOES have hundreds of useful plugins. I think we should start a conversation about how we can adapt and improve upon those plugins and make node even better - Matt Cashatt 2012-04-09 03:29
@mihai--Thanks for packaging linq.js. Can you please answer this question with the steps for packaging (in general) such a plugin for node and then I will gladly select your answer as the winner - Matt Cashatt 2012-04-09 03:35


-4

You don't.

You don't use jQuery on the server, ever. It has no place there, you don't have a DOM on the server and jQuery itself is a mediocre library to start with.

If you really want to use a "jQuery plugin" in node, you rewrite the plugin as a standalone module without a jQuery dependency.

As an aside, you also shouldn't need linq.js because it's an API you don't need, you already have array methods. Also your coding C# in JavaScript rather then learning JavaScript.

You also have all the array methods (map, filter, reduce, etc) so you simply do not need this. If you really want some of the sugar linq.js offers use underscore instead. (I personally recommend for ES5 over underscore)

Please use ECMAScript correctly rather then emulating C#.

2012-04-07 12:26
by Raynos
All due respect, your answer completely misses the mark and indicates that you didn't read the question in it's entirety. A couple of errors in your statement reflect simple misconceptions. For example, linq.js has nothing to do with SQL nor the DOM per se. Linq is one of the best technologies to come out of Microsoft IMO and it stands for "Language Integrated Querying". Linq allows, for example, Lambda expressions against runtime variables. In the case of linq.js, it can mean DOM elements or just JSON objects. You may consider opening your mind a bit to new possibilities - Matt Cashatt 2012-04-08 18:17
@MatthewPatrickCashatt all due respect but linq.js is a joke, don't use i - Raynos 2012-04-08 22:12
I can't help agreeing with @Raynos on the point that using jQuery on the server is sacrilege, particularly if you just want a plugin. He's just not argued the point very clearly. And on the point of linq.js, passing string arguments into methods like that is mighty ugly. It's a hack promoted by familiarity that would aid you if you were porting code directly, but it goes against the grain of js somewhat. But opinions aside, if you need linq.js then use the jQuery-less version at least, which has been package - Matt Esch 2012-04-08 22:23
@MatthewPatrickCashatt it stands for "Language Integrated Querying". -- Sure, that's the benefit of real LINQ -- it's integrated into the language. linq.js is not integrated into the language, it's just written in the language. Same with PHPLinq, linqj, and the others. That means instead of your query being composed of keywords and variables, like a normal expression, it's just a big string. Ignoring the fact that linq.js implicitly makes a bogus claim at language integration by naming itself "linq," can you give a concrete example of where it might actually be a useful abstraction - Dagg Nabbit 2012-04-08 22:49
Haters gonna hate but coming from Microsoft it should set off all alarm bells - Octavian Damiean 2012-04-08 22:56
@MattEsch-It was packaged only after I posted this question. See mihai's comment above. Ergo, some good came out of the question I asked and it prompted linq sans jQuery - Matt Cashatt 2012-04-09 03:22
@OctavianDamiean--normally I would agree; however, LINQ is one of those rare examples of a technology to come out of MS that, rather than seeming bloated, is the envy of other coding cultures. Just google "Linq for Java" if you need proof. Clearly I struck a nerve with folks over this question, but it served it's purpose and I am glad I asked it - Matt Cashatt 2012-04-09 03:26
@Raynos--One other thing to consider is that I am not proposing to "emulate" C# in JavaScript. Rather, I am proposing to extend JavaScript into something more robust that is not considered a joke to server-side coders (although I do not personally consider it a joke). My whole reason for asking this question is because I work on commercial applications that need to scale and need to package and search data in memory. I think node and linq.js can be powerful for that. Clearly JavaScript is your thing, why not embrace changes in the direction of expanding the importance of it - Matt Cashatt 2012-04-09 03:49
@MatthewPatrickCashatt data.filter(function(x) { return x[target] == comparer; }) - Raynos 2012-04-09 10:50
@GGG--filteredData = data.Where(function (x) { return x[target] == comparer; }); is pretty darn useful to me. As are the 'skip', 'take','distinct', 'intersect', 'groupby', and 'orderby' abstractions. These operations are abstracting away lower-level operations on JSON objects (in my case) such as binary searches, merge sorts and so on by allowing me to write them with a single line of code. What is so wrong with that - Matt Cashatt 2012-04-09 10:51
@Raynos--Thanks for the data.filter comment. That is useful. So is it your opinion that POJS can handle, in as few lines of code as linq.js, all of the other operations I am looking for such as 'skip', 'take','distinct', 'intersect', 'groupby', and 'orderby'? Obviously I am simply looking for abstractions that make querying data expedient and clean in code. Hopefully you don't have anything against abstractions in general because I don't think I could come to agreement with that, but if I have simply missed that POJS can do all of these things natively and easily please let me know - Matt Cashatt 2012-04-09 11:06
@MatthewPatrickCashatt it's my opinion that underscore as mentioned in the answer handles it more elegantly then linq.js. It's further my opinion that ES5 (POJS) is better then underscore, since I prefer slightly longer code that doesn't use foreign or confusing function - Raynos 2012-04-09 11:08
@MatthewPatrickCashatt The main issue is that your manipulating lists of data using SQL based commands like where, skip, take, distinict, etc. This is completely foreign and weird to JavaScript programmers. Instead you should use Array based commands like filter, map, forEach, reduce. See this answer for details of using POJS. Sure you can save a few lines by defining many niche macros for a particular action, but I prefer using fewer more readable building block - Raynos 2012-04-09 11:18
@Raynos--Thanks. Makes sense. You are correct about my drifting towards SQL which is natural for my background but maybe not the best idea for the JS paradigm. I will look into the Array based commands - Matt Cashatt 2012-04-09 11:27
It's not about emulating C#, it's about empowering Node developers to manipulate XML. Your answer completely contradicts reality. jsdom lets use use jQuery on the server. jQuery is a well-understood library for Javascript developers who aren't exposed to XML that much -- heck, it's great for everyone who doesn't want to deal with the raw W3C or XML DOM APIs. Combining jQuery and its XPath plugin is a valid way to expose XML manipulation to Node developers - laurelnaiad 2014-03-28 04:06
Ads