To quick start with Bun.js, follow these steps:
Install Bun. You can install Bun from the website or with your package manager.
Initialize a new project. Run
bun init
in an empty directory.Create a server. Create a file called
index.ts
and add the following code:
TypeScript
import { serve } from "bun";
const server = serve({
fetch() {
return new Response("Hello, world!");
},
port: 3000,
});
console.log("our server is running at " + server.port )
Start the server. Run
bun run
in the project directory.Open a web browser and navigate to
http://localhost:3000
. You should see the message "Hello, world!"
You can now modify the index.ts
file to create your own server application. Bun also includes a package manager, bundler, and test runner, so you can use it to build and run complete JavaScript applications.
Here is an example of how to install a package with Bun:
bun install cowsay
This will install the figlet
package and its type declarations. You can then use the figlet
package in your code:
TypeScript
import cowsay from "cowsay";
const cow = cowsay.say({
text: "Hello, world!",
cow: "default",
});
console.log(cow);
This will print the following output to the console:
(__)
(oo)
/------\/
* / | ||
~~ ~~
You can learn more about Bun on the official website: https://bun.sh/.