I remembered about quines today and as I hadn't ever written one before, I decided to write a quine in javascript for node.js. A quine is a computer program which takes no input and produces a copy of its own source code as its only output.

Turns out it was easier than expected. The key idea in writing a quine is having data that contains the whole program, which is then modified to match the program and printed.

Here is how my quine for node.js looks:

var data = "module.exports = function () { console.log(\"var data = \\\"%s\\\"\\n%s\", data.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, \"\\\\\\\"\"), data.replace(/{/, \"{\\n   \").replace(/}$/, \"\\n}\")); }"
module.exports = function () {
    console.log("var data = \"%s\"\n%s", data.replace(/\\/g, "\\\\").replace(/"/g, "\\\""), data.replace(/{/, "{\n   ").replace(/}$/, "\n}"));

If you require this module and call the exported function, you get the same module back:

> require('./index.js')()


var data = "module.exports = function () { console.log(\"var data = \\\"%s\\\"\\n%s\", data.replace(/\\\\/g, \"\\\\\\\\\").replace(/\"/g, \"\\\\\\\"\"), data.replace(/{/, \"{\\n   \").replace(/}$/, \"\\n}\")); }"
module.exports = function () {
    console.log("var data = \"%s\"\n%s", data.replace(/\\/g, "\\\\").replace(/"/g, "\\\""), data.replace(/{/, "{\n   ").replace(/}$/, "\n}")); 

Node-quine on my github: https://github.com/pkrumins/node-quine.


Turns out a quine in node.js that prints itself can be written much simpler this way (by maciejmalecki):

function quine() { console.log(quine.toString()) }

SubStack came up with this solution:

(function f() { console.log('(' + f.toString() + ')()') })()

And here is Erik Lundin's quine:

module.exports = function () {
    console.log('module.exports = ' + arguments.callee.toString());

Isaacs offers this version of qunie that adds cli support. However this doesn't count as quine is not allowed to read the source file of itself. But I'm including it here anyway as it's fun:

var data = require('fs').readFileSync(module.filename, 'utf8');
module.exports = function () {
if (module === require.main) module.exports();

Hooray for quines!


May 01, 2012, 15:08

I consider putting the source code as a string in the quine also to be somewhat cheating ;P

Here's another that doesn't:

var s = arguments.callee.toString().split('\n');
s[0] = s[0].split('{ ')[1];
s = s.join('\n');

module.exports = function () {
if (module === require.main) module.exports();

And, yes, the simplest quite *function* is:

function quine () { return quine.toString() }
May 01, 2012, 15:43

Nice one Isaac!

Here is another one by David Bjorklund:

function quine () { console.log('' + quine) }
May 01, 2012, 17:46

A variant of Erik's, consider the following which works in friebug or node or (hopefully) any JS interpreter...

(function() { return arguments.callee.toString(); })()

LungZeno Permalink
May 02, 2012, 04:29

quine.toString() takes the source code of quine itself as input.

Randolpho Permalink
May 04, 2012, 18:44

Remember: quines are programs, not functions. Here's a cleaner node.js quine a buddy of mine made:

var q = (exports = function () {
    process.nextTick(function () {
        console.log("var q = (exports = " + exports.toString() + ")();");
Randolpho Permalink
May 04, 2012, 18:46

And then he notices SubStack's better solution. Much nicer.

Lee Byron Permalink
October 31, 2013, 21:10

Here are two more JS quines (two variations on the same theme) for your enjoyment:



August 08, 2014, 03:59

Is so awesome plan

November 27, 2014, 17:28

Quine in node is good work of you sir. Bash redirection is a great technique for a healthy seo.
I will be using it now on on happy new year 2015 messages, happy new year greetings 2015, merry christmas 2014 wishes, happy new year 2015 wishes. Thanks a lot for this updates.

December 09, 2014, 10:26

a href=" http://mynycstreets.com/2014/12/download-lingaa-movie-torrentlingaa-movie-free-download/">Lingaa Movie Torrent Downl


lingaa movie in mp4lingaa movie in hdlingaa movie in free downloadLingaa Movie download in torrentfree download lingaa movie
diggnic article

December 11, 2014, 04:59

Artikel yang sangat menarik, terima kasih :)


December 11, 2014, 05:03

Informasi yang sangat bermanfaat, terima kasih =))


Diana Permalink
December 17, 2014, 16:42

This article is quite helpful and informative too. I enjoyed a lot. Thanks for sharing such a great article.

Beautiful Christmas Quotes for your friends and family...
christmas quotes
Christmas Messages for Whatsapp

Best Christmas Greetings for your friends and family...
christmas greeting
christmas wishes

What to Write in Christmas Card....... Check out best Christmas Greeting Card Words
What to Write in Christmas Card

Get Beautiful and Unique Christmas Wallpapers for free
free christmas wallpaper
christmas tree decorating ideas

Thanks for sharing such a great article.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "antispam_320": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.