The docker run command executes a command in a new container, pulling the image if necessary and then starting the container.
Key takeaways
Bun is a toolkit for JavaScript and TypeScript development, serving as an alternative to Node.js's
npm. It features a runtime based on Apple’s JavaScriptCore engine.The
bun runcommand executes JavaScript and TypeScript files, supporting various flags such as–watchfor real-time updates during development and–smolfor reduced memory usage at the cost of performance. It also allows running scripts defined inpackage.jsonfiles, similar tonpmoryarn.Bun maintains compatibility with Node.js conventions, making it easy for developers to transition. It handles conflicts between script keywords and built-in commands gracefully, providing a familiar and efficient environment for executing scripts and applications.
Bun is a toolkit designed to streamline the development process for JavaScript and TypeScript applications, acting as an alternative to Node.js's npm. It includes a runtime, which is the environment that allows you to execute JavaScript and TypeScript code. Unlike Node.js, which uses the V8 engine from Chromium, Bun's runtime is built on Apple's JavaScriptCore engine, which powers Safari. One of Bun’s key features is its CLI (command-line interface), which includes the bun run command. This command allows developers to execute scripts or files within Bun’s runtime environment, offering a faster and more efficient alternative for running JavaScript and TypeScript projects.
bun run commandWe can use this keyword in multiple ways. We can pass it the different files that we want to execute along with the flags that we want. Let's take a look at the different semantics of this command.
The syntax for executing a file using this command is as follows:
bun run <filename>
As compared to the node command, this command also takes the file name as an argument and supports all JavaScript and TypeScript file extensions. Alternatively, we can also omit the run in the command and use it as follows:
bun <filename>
A major difference between using bun run and node is that of performance. As Bun's runtime is written in
The –watch flag
This flag runs the file in watch mode. This enables any changes made in the file to be reflected in real time, without having to re-run the file. Here's how it is used with the command:
bun --watch run <filename>
Note: This command hard resets the entire started process and runs it from scratch. However, if we use the
–hotflag, Bun will only detect the changes made inside the code and update its internal module cache with the new code.
The –smol flag
This flag is used for environments with a constraint on memory. It reduces memory usage but this comes as a trade-off for less performance. Here's how we can use it:
bun --smol run <filename>
package.json filesSimilar to how we run files using npm or yarn, Bun also allows us to run package.json files and use the scripts defined inside the file. The scripts inside the package.json file are shell commands that Bun will execute in the root of the project directory. Here's how we can use bun run to run those scripts:
bun [bun flags] run <script> [script flags]
Let us assume that the scripts field inside our package.json file looks like the following:
{// ... other fields"scripts": {"clean": "rm -rf node_modules && echo 'Done.'","modules": "bun install","dev": "bun index.js"}}
We can use the following command to execute the commands defined by the keyword clean:
bun run clean
Note: If there's a conflict between the script keyword and a built-in
buncommand, the built-in command will take precedence. In this case, the entirebun run <script>command should be utilized instead ofbun <script>.
Oftentimes, the package.json scripts will contain references to locally installed CLIs such as vite or next. These are indicated by a '
bun run --bun <script>
You can try all of the commands you learned in the widget below:
{
"name": "bun-test-project",
"version": "1.0.0",
"description": "A sample project to test bun run commands.",
"main": "index.js",
"scripts": {
"clean": "rm -rf node_modules && echo 'Done.'",
"modules": "bun install",
"dev": "bun index.js"
},
"dependencies": {},
"devDependencies": {}
}Here's a recap of all the commands that you can test in the widget:
bun run <filename> / bun <filename>bun --watch run <filename>bun --smol run <filename>bun [bun flags] run <script> [script flags]bun run cleanbun run --bun <script>
In this Answer, we had a look at how we can use the bun run command to execute JavaScript and TypeScript files. We also saw how we can execute the scripts field inside the package.json file using the bun run command. It is evident that Bun's runtime is very similar to the very commonly used Node's runtime environment–this makes it extremely easy to understand and use.
Haven’t found what you were looking for? Contact Us
Free Resources