# add-variable-declarations

> Add variable declarations for global assignments.

Latest version **6.0.42** (published 2025-09-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install add-variable-declarations
pnpm add add-variable-declarations
yarn add add-variable-declarations
bun add add-variable-declarations
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 6.0.42 |
| Published | 2025-09-08 |
| First published | 2016-02-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 33.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Brian Donovan |
| Maintainers | eventualbuddha |
| Keywords | variable, declaration, ast, refactor |

## Links

- npm: https://www.npmjs.com/package/add-variable-declarations
- Repository: https://github.com/eventualbuddha/add-variable-declarations
- Issues: https://github.com/eventualbuddha/add-variable-declarations/issues
- npm.io page: https://npm.io/package/add-variable-declarations

## Dependencies (4)

- [@babel/types](https://npm.io/package/@babel/types.md) ^7.20.5
- [magic-string](https://npm.io/package/magic-string.md) ^0.30.0
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.20.5
- [@codemod/parser](https://npm.io/package/@codemod/parser.md) ^1.2.1

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

- 6.0.42 (latest) — 2025-09-08
- 6.0.41 — 2025-09-05
- 6.0.40 — 2025-08-22
- 6.0.39 — 2025-08-14
- 6.0.38 — 2025-07-24
- 6.0.37 — 2025-07-12
- 6.0.36 — 2025-07-02
- 6.0.35 — 2025-06-26
- 6.0.34 — 2025-06-06
- 6.0.33 — 2025-05-30
- 6.0.32 — 2025-05-27
- 6.0.31 — 2025-04-30
- 6.0.30 — 2025-03-24
- 6.0.29 — 2025-03-11
- 6.0.28 — 2025-03-10
- … 72 more at https://npm.io/package/add-variable-declarations/versions

## README

# add-variable-declarations

Add variable declarations for implicit global variable assignments.

## Install

```bash
# via yarn
$ yarn add add-variable-declarations
# via npm
$ npm install add-variable-declarations
```

## Usage

```js
import { addVariableDeclarations } from 'add-variable-declarations';

const source = `
PI = 3.14;
radius = 8;
circumference = 2 * PI * radius;
`;

console.log(addVariableDeclarations(source).code);
// var PI = 3.14;
// var radius = 8;
// var circumference = 2 * PI * radius;
```

## Development

1. Clone the repository.
   > `https://github.com/eventualbuddha/add-variable-declarations.git`
1. Install dependencies.
   > `yarn install`
1. Make edits and re-build.
   > `yarn run build`
1. Or, to watch and build files as you edit.
   > `yarn run build -- --watch`
1. Run tests to ensure everything works.
   > `yarn test`

## FAQ

### Why?

This project is used by [decaffeinate][decaffeinate] to add variable
declarations to converted CoffeeScript code. This project is not intended to
allow you to write JavaScript without variable declarations, as I consider that
a bad practice for the same reasons it's a bad practice in CoffeeScript.

[decaffeinate]: https://github.com/decaffeinate/decaffeinate

### Why `var`? Shouldn't this use `let` and `const`?

Yes, but not all situations allow the use of `let` and `const`, such as when
the variable binding takes advantage of
[hoisting](http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html).
If you want `let` and `const` you can use this library in combination with
[esnext](https://github.com/esnext/esnext) which will turn all eligible `var`
declarations into `let` or `const` as appropriate.

## License

MIT

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