Initialize a project
While it is possible to run scripts directly withdeno run, for largerprojects it is recommended to create a sensible directory structure. This wayyou can organize your code, manage dependencies, script tasks and run tests moreeasily.
Initialize a new project by running the following command:
deno init my_projectWheremy_project is the name of your project. You canread more about the project structure.
Run your projectJump to heading
Navigate to the project directory:
cd my_projectThen you can run the project directly using thedeno task command:
deno run devTake a look in thedeno.json file in your new project. You should see adevtask in the "tasks" field.
"tasks":{"dev":"deno run --watch main.ts"},Thedev task is a common task that runs the project in development mode. Asyou can see, it runs themain.ts file with the--watch flag, which willautomatically reload the script when changes are made. You can see this inaction if you open themain.ts file and make a change.
Run the testsJump to heading
In the project directory run:
denotestThis will execute all the tests in the project. You can read more abouttesting in Deno and we'll cover tests in alittle more depth in a later tutorial. At the moment you have one test file,main_test.ts, which tests theadd function inmain.ts.
Adding to your projectJump to heading
Themain.ts file serves as the entry point for your application. It’s whereyou’ll write your main program logic. When developing your project you willstart by removing the default addition program and replace it with your owncode. For example, if you’re building a web server, this is where you’d set upyour routes and handle requests.
Beyond the initial files, you’ll likely create additional modules (files) toorganize your code. Consider grouping related functionality into separate files.Remember that Denosupports ES modules, so youcan use import and export statements to structure your code.
Example folder structure for a deno project:
my_project/├── deno.json├── main.ts├── main_test.ts├── routes/│ ├── home.ts│ ├── about.ts├── services/│ ├── user.ts│ ├── post.ts└──utils/ ├── logger.ts ├── logger_test.ts ├── validator_test.ts └── validator.tsThis kind of structure keeps your project clean and makes it easier to find andmanage files.
🦕 Congratulations! Now you know how to create a brand new project withdeno init. Remember that Deno encourages simplicity and avoids complex buildtools. Keep your project modular, testable, and organized. As your projectgrows, adapt the structure to fit your needs. And most importantly, have funexploring Deno’s capabilities!