js-to-sh
raw JSON → 1.2.1 verified Fri May 01 auth: no javascript
js-to-sh (v1.2.1) is a JavaScript-to-ShellScript transpiler that uses abstract syntax trees (AST) to convert JavaScript syntax into bash scripts. It is experimental and unstable, with limited support for complex JavaScript features (classes are converted to functions but not fully implemented). The library provides a CLI tool and a programmatic API for transpiling .js/.ts files into .sh files. It includes built-in global helper functions for shellscript behaviors (file checks, command existence, etc.). The project is under active development with a small community, and it depends on Node.js >=20. Key differentiators: direct AST-based conversion, global variable injection for shell-style checks, and both ESM and CommonJS support.
Common errors
error SyntaxError: Unexpected token '.' ↓
cause Using optional chaining or nullish coalescing which is not supported by the transpiler.
fix
Rewrite code to avoid ?. and ?? operators, or manually handle in output script.
error TypeError: isCommand is not defined ↓
cause Forgot to import the side-effect module to inject global helper functions.
fix
Add
import 'js-to-sh' at the top of your entry file. error Error: Cannot find module 'js-to-sh' ↓
cause Package not installed globally or locally.
fix
Run
npm install js-to-sh (or npm install -g js-to-sh for CLI usage). error bash: syntax error near unexpected token `(' ↓
cause Transpiled script contains invalid shell syntax due to unsupported JS feature.
fix
Review generated .sh file; simplify original JavaScript to avoid complex constructs.
Warnings
gotcha Package is experimental; complex JS features (classes, async/await, modules) may produce incorrect or incomplete shell scripts. ↓
fix Test generated scripts thoroughly. Contribute to fix unsupported features.
breaking Global helper functions (isCommand, isDir, etc.) are only available after a side-effect import. They are not exported as named exports. ↓
fix Use `import 'js-to-sh'` or `require('js-to-sh')` once to inject globals.
deprecated CommonJS require is not recommended by the package; preferring ESM imports. ↓
fix Switch to ESM imports: `import { Transpiler } from 'js-to-sh'`.
gotcha CLI requires Node.js >=20. Older Node versions will fail to run. ↓
fix Upgrade Node.js to version 20 or higher.
Install
npm install js-to-sh yarn add js-to-sh pnpm add js-to-sh Imports
- Transpiler wrong
const { Transpiler } = require('js-to-sh')correctimport { Transpiler } from 'js-to-sh' - global side-effect wrong
import { isCommand } from 'js-to-sh'correctimport 'js-to-sh' - Transpiler type
import type { Transpiler } from 'js-to-sh'
Quickstart
// File: test.js
function add(a, b) {
return a + b;
}
console.log(add(1, 2));
// Terminal:
// npx js-to-sh -f test.js -o test.sh