Deno works as a sandbox, blocking all direct accesses with the operating system (Disk, Network, Env variables, etc.). To execute your deno app you have to grant permission explicitly depending on how much access it’s going to have.
For example, if you want to execute our deno-example project you should run this:
deno --allow-net --allow-read server.ts
--allow-net: allow network access
--allow-read: allow file system access
If you don’t specify the allow flags you will probably see an error like this on the console:
To display the list of available flags for permissions we can run the
deno run -h command.
CommonJS modules aren’t supported by Deno -it uses ESmodules as standard. Another breaking change is how dependencies are managed: package.json files are no longer necessary, and imports now are completely distributed instead of being in a centralized server like npm. The node_modules folder can be now disregarded. The new module system loads libraries directly from a URL, making explicit the extension of the file we want to use.
Remote code for external libraries is fetched and cached on first execution only. To update your dependencies you must use the run the
TypeScript is the default language for Deno projects, and the
tsc compiler forms part of his core. That saves us time since it’s supported without any additional configuration.
Deno vision embraces the idea of having a complete toolset available for common tasks to reduce time-wasting configuration, one good example of this might be:
Testing frameworks are in a good place these days, there are plenty of options out there like Jest, Mocha, and so on. Unit tests work like a quality assurance that ensures that everything should work as expected, deno includes a test runner between his built-in tools. Also, a couple of third-parties libraries are coming to help to create mocks, spies, and stubs.
For example, we are going to run the following unit test that simulates a Post creation.
We have to run
deno test [files] and indicate the files we want to test. In our case, the command looks like this
demo test tests/*.ts
Notice that we got an error because Deno prevents the code execution whether we don’t use the required flags, we can give another try using the
deno test --allow-read tests/*.ts. This time it isn’t necessary to use the
--allow-net flag because the
fetch method was mocked in our unit test.
The code format is fundamental these days between developer teams, as it helps to keep consistency around how the code looks like, tools such as
Prettier are well known to facilitate this task. Deno incorporates its own auto-format source code within its utilities.
For example, a code like this one doesn’t follow any formatting style, it lacks indentation and it’s missing semicolons in some places.
deno fmt command the code is formatted
Deno provides two essential types of libraries:
- Standard: They don’t have any external dependencies, they’re maintained by the core Deno team directly. It compounds from operating system manage like
fs, http, and path to utility libraries like
testing and log, among others.
- Third-Party: They are created by the community, decentralized and can be hosted on the public deno directory at deno.land