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 projecttypescript
pnpm init
pnpm add -D typescript
npx tsc --initI use pnpm as package manager.
Running the commands above will install typescript and generate the default package.json and tsconfig.json.
effectrequires 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:
{
"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
noUncheckedIndexedAccessfor full type safety.With
noUncheckedIndexedAccessaccessing an element from an arrayT[]will returnT | undefinedinstead of justT.
Installing effect
We are now ready to install effect:
pnpm add effectCreate a src folder and add an index.ts file inside it with the following code:
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:
tsxallows to run.tsfiles similar to how thenodecommand works for.js.
pnpm add -D tsxWe then create a dev script that uses tsx to run src/index.ts:
{
"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 devYou will see something like this in the console:
> effect-getting-started-course@1.0.0 dev
> tsx src/index.ts
Hello worldSince we are going to work with node we also need to install node types from @types/node:
pnpm add -D @types/nodeThis adds types for fetch, Request, Response, and more.
