# JavaScript and TypeScript with Elide

> Run your first TypeScript program, install and import an npm dependency, and test it with node:test; no compile step or tsconfig.json.

Elide runs JavaScript and TypeScript directly, with CommonJS, ES modules, and a subset of the Node and Web APIs.
This quickstart builds a small greeting program, adds an npm dependency, and tests the greeting.

Bringing an existing app? Start with [Elide for Node users](https://elide.help/docs/elide-for-node) and the
[framework compatibility table](https://elide.help/docs/http-service#frameworks).

## Install

[Install Elide](https://elide.help/docs/installation), open a new terminal, then check:

```bash
elide --version
```

If the command is not found, follow [Verify the install](https://elide.help/docs/installation#verify-the-install).

## Run a file

> [!TIP] TypeScript runs as written
> No compile step and no `tsconfig.json`: `elide app.ts` runs the file.

```bash
mkdir hello-elide
cd hello-elide
```

Save this as `app.ts`:

```ts
const name: string = "Ada";
console.log(`Hello, ${name}!`);
```

```bash
elide run app.ts
```

```terminaloutput
Hello, Ada!
```

You can also use `elide app.ts`. For JavaScript, remove `: string` and save the file as `app.js`.

<img src="https://elide.help/assets/demos/run-ts.gif" alt="Running a TypeScript file directly with Elide" width="1591" height="982" loading="lazy" decoding="async" class="rounded-lg border border-border my-6 w-full" />

## npm packages

Create `package.json` in the same directory:

```json
{
  "type": "module",
  "dependencies": { "zod": "3.24.1" }
}
```

Install the dependency:

```bash
elide install --ecosystems=npm
```

Plain `elide install` installs Maven dependencies and skips npm. If an import cannot find its package, check
[Module not found after `elide install`](https://elide.help/docs/troubleshooting#module-not-found-after-elide-install).

Save the greeting logic as `greeting.ts`:

```ts
import { z } from "zod";

const User = z.object({ name: z.string() });

export function greeting(user: unknown): string {
  return `Hello, ${User.parse(user).name}!`;
}
```

Replace `app.ts` with:

```ts
import { greeting } from "./greeting.ts";

console.log(greeting({ name: "Ada" }));
```

```bash
elide run app.ts
```

```terminaloutput
Hello, Ada!
```

This confirms the installed package imports and validates the input.

<img src="https://elide.help/assets/demos/npm-install.gif" alt="Installing npm dependencies and importing them from TypeScript" width="1591" height="764" loading="lazy" decoding="async" class="rounded-lg border border-border my-6 w-full" />

Packages go into `node_modules`. If the project also has Maven dependencies, use `--ecosystems=maven,npm` or
`--workspace`. Lockfiles and options: [NPM Installer](https://elide.help/docs/toolchain/web/npm).

## Tests

Create `greeting.test.ts` beside the other files. `elide test` discovers `*.test.ts` files and runs `node:test` tests:

```ts
import test from "node:test";
import assert from "node:assert";
import { greeting } from "./greeting.ts";

test("greets a named user", () => {
  assert.equal(greeting({ name: "Ada" }), "Hello, Ada!");
});

test("rejects a user without a name", () => {
  assert.throws(() => greeting({}));
});
```

```bash
elide test
```

Both tests should pass. If no tests are found, check the filename and run the command from `hello-elide`.
More discovery rules and runner options: [Testing](https://elide.help/docs/toolchain/testing).

## Servers

Continue with [Build an HTTP service](https://elide.help/docs/http-service) for a complete server you can run and query.
For an existing directory of web files:

```bash
elide serve --no-tui ./dist # static files on 127.0.0.1:8080
elide dev --no-tui ./src    # port 3000; pages reload on change
```

Given a script, `serve` and `dev` run it; a module that exports a `fetch` handler is served directly. See
[Development Server](https://elide.help/docs/dev-server).

## Executable scripts

On macOS and Linux, start a file with `#!/usr/bin/env elide` and mark it executable:

```ts
#!/usr/bin/env elide
const name: string = process.argv[2] ?? "world";
console.log(`Hello, ${name}!`);
```

Save it as `hello.ts`, then run:

```bash
chmod +x hello.ts
./hello.ts Ada
```

The output is `Hello, Ada!`. The same first line works for `.js`, `.mjs` and `.py` files.

## Samples

- [TypeScript without a build](https://elide.help/docs/code-samples#typescript-without-a-build)
- [HTTP handler](https://elide.help/docs/code-samples#http-handler)
- [TypeScript tests](https://elide.help/docs/code-samples#typescript-tests)
- [Local server and fetch](https://elide.help/docs/code-samples#local-server-and-fetch)

## More

Extensions: `.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mts`, `.cts`, `.tsx`. JSX and TSX are transformed; install the
runtime they import, such as `react/jsx-runtime`. Elide provides no UI framework or DOM. See
[TypeScript and modules](https://elide.help/docs/typescript-and-modules) for module resolution and type checking.

Implemented Node and Web modules: [API Overview](https://elide.help/docs/api-overview). SQLite, PostgreSQL and MySQL drivers:
[Databases](https://elide.help/docs/api-database). Measured results:
[Compatibility Matrix](https://elide.help/docs/compatibility). Traces, metrics, and logs through `@opentelemetry/api`, with no SDK:
[OpenTelemetry in JavaScript](https://elide.help/docs/opentelemetry/javascript).
