Big update!Introducing GitHub Bot Commands. Learn more
Log inBook a demo


Package Overview
File Explorer

Advanced tools


Serialize JavaScript to a superset of JSON that includes regular expressions and functions.


Version published
Weekly downloads
decreased by-10.51%

Weekly downloads




  • Add support for URL's (#123)
  • Bump mocha from 9.0.0 to 9.0.1 (#124)
  • Bump mocha from 8.4.0 to 9.0.0 (#121)
  • Update Node.js CI matrix (#122)
  • Bump mocha from 8.3.2 to 8.4.0 (#120)
  • Bump lodash from 4.17.19 to 4.17.21 (#119)
  • Bump y18n from 4.0.0 to 4.0.1 (#116)
  • Bump chai from 4.3.3 to 4.3.4 (#115)
  • Bump mocha from 8.3.1 to 8.3.2 (#114)
  • Bump mocha from 8.3.0 to 8.3.1 (#113)
  • Bump chai from 4.3.1 to 4.3.3 (#112)
  • Bump chai from 4.2.0 to 4.3.1 (#111)
  • Bump mocha from 8.2.1 to 8.3.0 (#109)
  • Bump mocha from 8.1.3 to 8.2.1 (#105)
  • Drop Travis CI settings (#100)
  • Change default branch name to main (#99)
  • GitHub Aactions (#98)

Behavior changes for URL objects

It serializes URL objects as follows since this version. The result of serialization may be changed if you are passing URL object values into the serialize-javascript.

const serialize = require("serialize-javascript"); serialize({u: new URL("")}); // '{"u":new URL("")}'

Thank you @rrdelaney for this release.



Serialize JavaScript

Serialize JavaScript to a superset of JSON that includes regular expressions, dates and functions.

npm Version Dependency Status Test


The code in this package began its life as an internal module to express-state. To expand its usefulness, it now lives as serialize-javascript — an independent package on npm.

You're probably wondering: What about JSON.stringify()!? We've found that sometimes we need to serialize JavaScript functions, regexps, dates, sets or maps. A great example is a web app that uses client-side URL routing where the route definitions are regexps that need to be shared from the server to the client. But this module is also great for communicating between node processes.

The string returned from this package's single export function is literal JavaScript which can be saved to a .js file, or be embedded into an HTML document by making the content of a <script> element.

HTML characters and JavaScript line terminators are escaped automatically.

Please note that serialization for ES6 Sets & Maps requires support for Array.from (not available in IE or Node < 0.12), or an Array.from polyfill.


Install using npm:

$ npm install serialize-javascript


var serialize = require('serialize-javascript'); serialize({ str : 'string', num : 0, obj : {foo: 'foo'}, arr : [1, 2, 3], bool : true, nil : null, undef: undefined, inf : Infinity, date : new Date("Thu, 28 Apr 2016 22:02:17 GMT"), map : new Map([['hello', 'world']]), set : new Set([123, 456]), fn : function echo(arg) { return arg; }, re : /([^\s]+)/g, big : BigInt(10), });

The above will produce the following string output:

'{"str":"string","num":0,"obj":{"foo":"foo"},"arr":[1,2,3],"bool":true,"nil":null,"undef":undefined,"inf":Infinity,"date":new Date("2016-04-28T22:02:17.000Z"),"map":new Map([["hello","world"]]),"set":new Set([123,456]),"fn":function echo(arg) { return arg; },"re":new RegExp("([^\\\\s]+)", "g"),"big":BigInt("10")}'

Note: to produced a beautified string, you can pass an optional second argument to serialize() to define the number of spaces to be used for the indentation.

Automatic Escaping of HTML Characters

A primary feature of this package is to serialize code to a string of literal JavaScript which can be embedded in an HTML document by adding it as the contents of the <script> element. In order to make this safe, HTML characters and JavaScript line terminators are escaped automatically.

serialize({ haxorXSS: '</script>' });

The above will produce the following string, HTML-escaped output which is safe to put into an HTML document as it will not cause the inline script element to terminate:


You can pass an optional unsafe argument to serialize() for straight serialization.


The serialize() function accepts an options object as its second argument. All options are being defaulted to undefined:

This option is the same as the space argument that can be passed to JSON.stringify. It can be used to add whitespace and indentation to the serialized output to make it more readable.

serialize(obj, {space: 2});

This option is a signal to serialize() that the object being serialized does not contain any function or regexps values. This enables a hot-path that allows serialization to be over 3x faster. If you're serializing a lot of data, and know its pure JSON, then you can enable this option for a speed-up.

Note: That when using this option, the output will still be escaped to protect against XSS.

serialize(obj, {isJSON: true});

This option is to signal serialize() that we want to do a straight conversion, without the XSS protection. This options needs to be explicitly set to true. HTML characters and JavaScript line terminators will not be escaped. You will have to roll your own.

serialize(obj, {unsafe: true});

This option is to signal serialize() that we do not want serialize JavaScript function. Just treat function like JSON.stringify do, but other features will work as expected.

serialize(obj, {ignoreFunction: true});


For some use cases you might also need to deserialize the string. This is explicitly not part of this module. However, you can easily write it yourself:

function deserialize(serializedJavascript){ return eval('(' + serializedJavascript + ')'); }

Note: Don't forget the parentheses around the serialized javascript, as the opening bracket { will be considered to be the start of a body.


This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.



What is serialize-javascript?

Serialize JavaScript to a superset of JSON that includes regular expressions and functions.

Is serialize-javascript popular?

The npm package serialize-javascript receives a total of 23,913,375 weekly downloads. As such, serialize-javascript popularity was classified as popular.

Is serialize-javascript well maintained?

We found that serialize-javascript demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.It has 3 open source maintainers collaborating on the project.

Last updated on 21 Jun 2021

Did you know?

Socket installs a Github app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

Install Socket


Subscribe to our newsletter

Get open source security insights delivered straight into your inbox. Be the first to learn about new features and product updates.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc