安装
一步步教你如何在 Node.js、Deno、Bun 以及 Vite + React 等不同平台上搭建新的 Effect 项目。
环境要求:
- TypeScript 5.4 或更高版本。
- 支持 Node.js 22.18 或更高版本、Deno 以及 Bun。
手动安装
JavaScript 运行时
按照以下步骤为 Node.js、Bun 或 Deno 创建一个新的 Effect 项目:
-
创建一个项目目录并进入其中:
mkdir hello-effect cd hello-effect -
初始化一个 TypeScript 项目:
npmnpm init -y npm install --save-dev typescriptpnpmpnpm init pnpm add --save-dev typescriptYarnyarn init -y yarn add --dev typescriptBunbun initDenodeno init这会创建一个
package.json文件,为你的 TypeScript 项目提供初始配置。对于 Bun,这还会生成一个tsconfig.json文件;对于 Deno,则会生成一个deno.json文件。请确保
package.json文件中包含"type": "module"字段,这样 Node.js 才会把你的源文件当作 ES 模块处理(bun init会自动添加该字段):{ "type": "module" } -
初始化 TypeScript:
npmnpx tsc --initpnpmpnpm tsc --initYarnyarn tsc --initBunbun init已经生成了一个tsconfig.json文件。DenoDeno 开箱即用地运行 TypeScript,并且
deno init已经生成了一个deno.json文件,其中默认启用了strict模式。无需再做其他配置。运行该命令后,会生成一个
tsconfig.json文件,其中包含 TypeScript 的配置选项。最需要重视的选项之一是strict标志。请务必打开
tsconfig.json文件,确认strict选项的值被设置为true。{ "compilerOptions": { "strict": true } } -
把所需的包安装为依赖:
npmnpm install effectpnpmpnpm add effectYarnyarn add effectBunbun add effectDenodeno add npm:effect这个包会为你的 Effect 项目提供基础功能。
让我们编写并运行一个简单的程序,确认一切都已经正确配置。
在终端中执行以下命令:
mkdir src
touch src/index.ts
打开 src/index.ts 文件并添加以下代码:
import { Effect, Console } from "effect"
const program = Console.log("Hello, World!")
Effect.runSync(program)
运行 src/index.ts 文件。Node.js 22.18 或更高版本、Bun 与 Deno 都可以直接运行 TypeScript 文件,因此不需要额外的工具链:
node src/index.tsnode src/index.tsnode src/index.tsbun src/index.tsdeno run src/index.ts如果你使用的是较旧版本的 Node.js,可以改用 tsx 运行该文件:npx tsx src/index.ts。
你应该会看到打印出 "Hello, World!" 消息。这说明程序运行正常。
Vite + React
按照以下步骤为 Vite + React 创建一个新的 Effect 项目:
-
搭建 Vite 项目脚手架:打开终端并运行以下命令:
npm# npm 6.x npm create vite@latest hello-effect --template react-ts # npm 7+, extra double-dash is needed npm create vite@latest hello-effect -- --template react-tspnpmpnpm create vite@latest hello-effect -- --template react-tsYarnyarn create vite@latest hello-effect -- --template react-tsBunbun create vite@latest hello-effect -- --template react-tsDenodeno init --npm vite@latest hello-effect -- --template react-ts该命令会创建一个使用 React 与 TypeScript 模板的新 Vite 项目。
-
进入新建的项目目录并安装所需的包:
npmcd hello-effect npm installpnpmcd hello-effect pnpm installYarncd hello-effect yarn installBuncd hello-effect bun installDenocd hello-effect deno install安装完成后,打开
tsconfig.json文件,确认strict选项的值被设置为 true。{ "compilerOptions": { "strict": true } } -
把所需的包安装为依赖:
npmnpm install effectpnpmpnpm add effectYarnyarn add effectBunbun add effectDenodeno add npm:effect这个包会为你的 Effect 项目提供基础功能。
现在,让我们编写并运行一个简单的程序,确认一切都已经正确配置。
打开 src/App.tsx 文件,并用以下代码替换其内容:
+import { useState, useMemo, useCallback } from "react"
import reactLogo from "./assets/react.svg"
import viteLogo from "/vite.svg"
import "./App.css"
+import { Effect } from "effect"
function App() {
const [count, setCount] = useState(0)
+ const task = useMemo(
+ () => Effect.sync(() => setCount((current) => current + 1)),
+ [setCount]
+ )
+
+ const increment = useCallback(() => Effect.runSync(task), [task])
return (
<>
<div>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
+ <button onClick={increment}>count is {count}</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
}
export default App
完成这些修改后,运行以下命令启动开发服务器:
npm run devpnpm run devyarn run devbun run devdeno run dev然后按 o 在浏览器中打开应用。
点击按钮时,你应该会看到计数器递增。这说明程序运行正常。