Create the project

In this first lesson we are going to setup the files and folders for a minimal program with effect:

  • package.json: configuration for any nodejs project
  • typescript
pnpm init
pnpm add -D typescript
npx tsc --init

I use pnpm as package manager.

Running the commands above will install typescript and generate the default package.json and tsconfig.json.

effect requires typescript 5.0 or newer

When working with effect is important to set strict: true in your tsconfig.json. The other parameters will mostly depend on your project.

I used tsconfig.guide to generate the below tsconfig.json:

tsconfig.json
{
  "compilerOptions": {
    "esModuleInterop": true,
    "skipLibCheck": true,
    "target": "es2022",
    "allowJs": true,
    "resolveJsonModule": true,
    "moduleDetection": "force",
    "isolatedModules": true,
    "verbatimModuleSyntax": true,
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "module": "preserve",
    "noEmit": true,
    "lib": ["es2022"]
  },
  "include": ["**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

This is all we need as configuration.

I strongly suggest you to enable also noUncheckedIndexedAccess for full type safety.

With noUncheckedIndexedAccess accessing an element from an array T[] will return T | undefined instead of just T.

Installing effect

We are now ready to install effect:

pnpm add effect

Create a src folder and add an index.ts file inside it with the following code:

index.ts
import { Console, Effect } from "effect";

const main = Console.log("Hello world");

Effect.runSync(main);

Bare with me for now, we are going to understand the above code in the next lesson.

For now let's focus on getting something up and running!

Running the program

The final step is running the program.

The easiest option out there is tsx:

tsx allows to run .ts files similar to how the node command works for .js.

pnpm add -D tsx

We then create a dev script that uses tsx to run src/index.ts:

package.json
{
  "name": "effect-getting-started-course",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "dev": "tsx src/index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "tsx": "^4.16.2",
    "typescript": "^5.5.3"
  },
  "dependencies": {
    "effect": "^3.4.7"
  }
}

Finally we can run the program:

pnpm run dev

You will see something like this in the console:

> effect-getting-started-course@1.0.0 dev
> tsx src/index.ts

Hello world
Effect Playground

Since we are going to work with node we also need to install node types from @types/node:

pnpm add -D @types/node

This adds types for fetch, Request, Response, and more.