# node-source-walk

> Execute a callback on every node of a source code's AST and stop walking when you see fit

Latest version **8.0.0** (published 2026-05-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-source-walk
pnpm add node-source-walk
yarn add node-source-walk
bun add node-source-walk
```

## Health

**Score 55/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 8.0.0 |
| Published | 2026-05-19 |
| First published | 2014-03-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=20.19.0 \|\| >=22.12.0 |
| Dependencies | 1 |
| Unpacked size | 9.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 64 |
| Author | Joel Kemp |
| Maintainers | pahen, mrjoelkemp, xhmikosr |
| Keywords | ast, traversal, acorn, static analysis, source code, walker, jsx |

## Links

- npm: https://www.npmjs.com/package/node-source-walk
- Repository: https://github.com/dependents/node-source-walk
- Issues: https://github.com/dependents/node-source-walk/issues
- npm.io page: https://npm.io/package/node-source-walk

## Dependencies (1)

- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.29.3

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 8.0.0 (latest) — 2026-05-19
- 7.0.2 — 2026-04-25
- 7.0.1 — 2025-01-30
- 7.0.0 — 2024-04-08
- 6.0.2 — 2023-05-15
- 6.0.1 — 2023-05-08
- 6.0.0 — 2023-05-04
- 5.0.2 — 2023-04-26
- 5.0.1 — 2023-03-19
- 5.0.0 — 2022-03-02
- 4.3.0 — 2022-02-22
- 4.2.2 — 2022-02-19
- 4.2.1 — 2022-02-19
- 4.2.0 — 2018-12-14
- 4.1.0 — 2018-10-27
- … 20 more at https://npm.io/package/node-source-walk/versions

## README

# node-source-walk

[![CI](https://img.shields.io/github/actions/workflow/status/dependents/node-source-walk/ci.yml?branch=main&label=CI&logo=github)](https://github.com/dependents/node-source-walk/actions/workflows/ci.yml?query=branch%3Amain)
[![npm version](https://img.shields.io/npm/v/node-source-walk?logo=npm&logoColor=fff)](https://www.npmjs.com/package/node-source-walk)
[![npm downloads](https://img.shields.io/npm/dm/node-source-walk)](https://www.npmjs.com/package/node-source-walk)

> Synchronously execute a callback on every node of a file's AST and stop walking whenever you see fit.

```sh
npm install node-source-walk
```

## Usage

## ESM

```js
import Walker from 'node-source-walk';

const walker = new Walker();

// Assume src is the string contents of myfile.js
// or the AST of an outside parse of myfile.js
walker.walk(src, node => {
  if (node.type === whateverImLookingFor) {
    // No need to keep traversing since we found what we wanted
    walker.stopWalking();
  }
});
```

### CommonJS

```js
const { default: Walker } = require('node-source-walk');

const walker = new Walker();
// ...
```

By default, Walker will use `@babel/parser` (supporting ES6, JSX, Flow, and all other available `@babel/parser` plugins) and the `sourceType: module`, but you can change any of the defaults as follows:

```js
const walker = new Walker({
  sourceType: 'script',
  // If you don't like experimental plugins
  plugins: [
    'jsx',
    'flow'
  ]
});
```

* The supplied options are passed through to the parser, so you can configure it according to `@babel/parser`'s [documentation](https://babeljs.io/docs/en/babel-parser.html).

## Swap out the parser

If you want to supply your own parser, you can do:

```js
const walker = new Walker({
  parser: mySweetParser
});
```

* The custom parser must have a `.parse` method that takes in a string and returns an object/AST.
* All of the other options supplied to the Walker constructor will be passed along as parser options to your chosen parser.

## API

### `walk(src, callback)`

* Recursively walks the given `src` from top to bottom
* `src`: the contents of a file **or** its (already parsed) AST
* `callback`: a function that is called for every visited node
  * The argument passed to `callback` will be the currently visited node.

### `moonwalk(node, callback)`

* Recursively walks up an AST starting from the given node. This is a traversal that's in the opposite direction of `walk` and `traverse`
* `node`: a valid AST node
* `callback`: a function that is called for every node (specifically via visiting the parent(s) of every node recursively)
  * The argument passed to `callback` will be the currently visited node.

### `stopWalking()`

* Halts further walking of the AST until another manual call of `walk` or `moonwalk`
* This is super-beneficial when dealing with large source files (or ASTs)

### `traverse(node, callback)`

* Allows you to traverse an AST node and execute a callback on it
* Callback should expect the first argument to be an AST node, similar to `walk`'s callback

### `parse(src)`

* Uses the options supplied to Walker to parse the given source code string and return its AST using the configured parser (or `@babel/parser` by default).

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/node-source-walk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
