How to Create a Fresh Project in Deno Quickly
To create a fresh project in
deno, start by creating a new folder and adding a main script file like mod.ts. Then run your code with deno run and use deno task to manage scripts. Deno does not require a package manager or config file to start.Syntax
Creating a fresh Deno project mainly involves these steps:
- Create a folder: This will hold your project files.
- Add a main script file: Usually named
mod.tsormain.ts. - Run your script: Use
deno run [options] filename.tsto execute your code. - Optional tasks: Use
deno taskto define and run scripts from adeno.jsonconfig file.
Deno uses URL imports or local files directly, so no node_modules or package.json is needed.
bash
mkdir my-deno-project
cd my-deno-project
echo "console.log('Hello Deno!')" > mod.ts
deno run mod.tsOutput
Hello Deno!
Example
This example shows how to create a simple Deno project that prints a greeting. It demonstrates creating a file, writing code, and running it.
typescript
console.log('Welcome to your fresh Deno project!');
Output
Welcome to your fresh Deno project!
Common Pitfalls
Beginners often expect Deno to use package.json or node_modules like Node.js, but Deno does not. Also, forgetting to add --allow-net or other permissions when running scripts that access network or files causes errors.
Another common mistake is not using the .ts or .js extension in import statements, which Deno requires explicitly.
typescript
/* Wrong: Missing file extension in import */ import { serve } from "https://deno.land/std/http/server"; /* Right: Include file extension */ import { serve } from "https://deno.land/std/http/server.ts";
Quick Reference
Summary tips for starting a Deno project:
- Create a folder and add
mod.tsas your entry file. - Run scripts with
deno run mod.ts. - Use
deno taskwithdeno.jsonfor script automation. - Always specify file extensions in imports.
- Grant permissions explicitly when needed (e.g.,
--allow-net).
Key Takeaways
Start a Deno project by creating a folder and a main script file like mod.ts.
Run your code with deno run and specify needed permissions explicitly.
Deno does not use package.json or node_modules; imports need full file extensions.
Use deno task and deno.json to manage scripts and tasks easily.
Always check permissions and import paths to avoid common errors.