6

I want to use some features for server-side javascript. I think prototype is checking the browser type, but of course node.js is not a browser. I get the following error:

$ node
> require('./prototype') ;
ReferenceError: navigator is not defined
    at /home/guest/projects/javascript/prototype.js:14:5
    at Object.<anonymous> (/home/guest/projects/javascript/prototype.js:23:4)
    at Module._compile (node.js:462:23)
    at Module._loadScriptSync (node.js:469:10)
    at Module.loadSync (node.js:338:12)
    at loadModule (node.js:283:14)
    at require (node.js:411:14)
    at cwdRequire (repl:29:10)
    at [object Context]:1:1
    at Interface.<anonymous> (repl:96:19)

prototype.js is version 1.7, node.js is version 0.2.6

projectshave
  • 3,771
  • 5
  • 23
  • 24

4 Answers4

6

Prototype is written to be modular. This means you can use just the useful parts that extend Array and Class and Function (I love those bits!) and leave out the parts that deal with browser and DOM (the bits that are slow in IE and non-existent in node).

Start by going to https://github.com/sstephenson/prototype then pick out the desired parts from src/prototype/ and src/prototype/lang/.

I wish you luck on such a fascinating challenge.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
4

Late answer, but I'm sure it can still be useful to some people:

https://github.com/Rixius/prototype.node.js

Few days ago, I did something like that myself, and realized it had already been done... This repo is hard to find even with the github search.

Robin
  • 21,667
  • 10
  • 62
  • 85
  • I just fixed the name of this repo to prototype.node.js... Honestly I made it just to prove to myself it was possible. New link is https://github.com/Rixius/prototype.node.js – Rixius Jun 21 '11 at 16:34
  • I haven't touched this in quite a while, but I'm starting it up to make it work on a recent build of node and make sure it works right. – Rixius Jun 21 '11 at 16:39
  • When I used it a few weeks ago, it worked perfectly with the last node version, except that I had to replace Hash with require('Hash') somewhere (or something like that) :) – Robin Jun 21 '11 at 21:25
2

If you have a look at the source of Prototype.js, it is tightly bound to the browser environment, which isn't provided by node (since it's not a web browser).

jsdom attempts to mock the browser environment, and has been used to successfully run JQuery on the server side. Your mileage may vary.

Community
  • 1
  • 1
Stoive
  • 11,232
  • 4
  • 24
  • 32
  • In fact, everyone mileage with jsdom may vary with every version of jsdom. Last time I tried to use it on google.com, it simply exploded. It really shows to great extend how broken the Browser environment really is. – Ivo Wetzel Feb 10 '11 at 06:21
0

There is underscore.js especially for node.js, which implements most of Prototypes beloved functions:

Underscore is a utility-belt library for JavaScript that provides a lot of  
the functional programming support that you would expect in Prototype.js.

Its faster as Prototype itself, because it does not extend any of the built-in JavaScript objects.
Due to this, the syntax is slightly different:

// prototype.js:
anArray.each(function(){ ... });
// underscore.js:
_ = require('underscore');
_.each(anArray, function(){ ... });

If your are looking for Prototypes String functions like trim, have a look at underscore.string

RienNeVaPlu͢s
  • 7,442
  • 6
  • 43
  • 77