Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including astronaut with all npm packages installed. Try it out:

var astronaut = require("astronaut")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

astronaut v0.1.4

A library for transforming Esprima/SpiderMonkey ASTs.


Astronaut is a library for traversing and manipulating the javascript AST. Build Status


Let's replace all instances of 5 with 4:

astronaut('1 + 5')
    .walk(function(node) { 
        if (node.isLiteral() 
                && node.value() === 5) { 
//'1 + 4'

Let's replace all instances of 5 with a call to the function "f":

astronaut('1 + 5')
    .walk(function(node) { 
        if (node.isLiteral() 
                && node.value() === 5) { 
//'1 + f()'

Or how about we wrap them in a call to the function f:

astronaut('1 + 5')
    .walk(function(node) { 
        if (node.isLiteral() 
                && node.value() === 5) { 
            node.wrap('f(<%= node %>)');
//'1 + f(5)'

Let's prefix all calls to f where the second argument is an array expression with a call to g and the same array:

var code = "f(); f(1, [1,2,3]);";
astronaut(code).walk(function(node) {
    if (node.isCallExpression() 
            && node.calleeName() === "f"
            && node.arguments().length > 1
            && node.arguments()[1].isArrayExpression()) {
        node.prefix("g(" + node.arguments()[1].deparse() + ')');

Let's wrap a function body in a try/catch:

var code = "function f(a) { return a; }";
astronaut(code).walk(function(node) {
    if (node.isBlockStatement() && node.parent.isFunctionDeclaration()) {
        node.wrap("try { <%= node %> } catch(e) {} }");
//"function f(a) { try { return a; } catch(e) {} }";



var astronaut = require('astronaut')

Astronaut takes a string to be parsed (using Esprima), or an already-parsed Esprima/SpiderMonkey AST, and returns an AstNode.


The SpiderMonkey AST data for this node, with sub-nodes wrapped in AstNodes. Updates to this mutate the tree, but for non-primitive values it's recommended to use the replace/wrap functions.


A pointer to the parent of this node


The key in the parent that points to this node.


If parent[key] is an array, the index of this node in that array. This enables affix functions. <NodeType>()

Returns a boolean indicating whether or note the node is of the specified type.


Returns the SpiderMonkey AST for this node.


Walk the AST starting at this node, calling the callback along the way.

Walk the tree, replacing nodes with new nodes produced by callback(node). To avoid thrashing the tree, if the callback returns null/undefined, no replacement occurs.

AstNode.reduce(accumulator, callback)

Reduce the tree down to a single value. callback should be a function that expects the accumulator as the first argument, the current node as the second, and returns the new accumulator.


A shortcut for escodegen.generate(AstNode.ast(), options)


Wrap the current node in the code specified by an underscore template. The template can be a string to be parsed or a compiled template. The current node should be represented as node in the template. For example: node.wrap("f(<%= node =>)")


Replace the current node with the results of parsing the input code


Insert the statement specified in code prior to the statement encoded by the current node.


Insert the statement specified in code after the statement encoded by the current node.


npm install astronaut


I was inspired by burrito, but was dissapointed by the lack of child pointers and wanted something that supported the Esprima AST. Falafel seems like it may have solved both problems.


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free