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

1
  • What exactly do you want to use it for? Commented Feb 10, 2011 at 5:25

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It looks like src/prototype/lang.js is a Sprocket file to include only the language bits I want.
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.

3 Comments

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 github.com/Rixius/prototype.node.js
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.
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) :)
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.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.