> For the complete documentation index, see [llms.txt](https://kdongs.gitbook.io/kdocs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdongs.gitbook.io/kdocs/deno/about.md).

# About

Deno can run JavaScript and TypeScript with no additional tools or configuration required, unlike `Node` which will require some additional libraries and configurations to work with it.

Additionally Deno by default can work as `HTTP Server`, `Routes`, `Database access`, and more.

{% embed url="<https://docs.deno.com/>" %}

## Installing Deno

Installing on Linux is simple:

```bash
curl -fsSL https://deno.land/install.sh | sh
```

Check verion with:

```bash
deno --version
```

## Deno Project

Deno configuration file is `deno.json` which is similar to the `package.json` from Node.

It will automatically recognize this file in project root folder, but an arbitrary path to the configuration file can be passed through the `--config` flag when executing Deno.

{% hint style="info" %}
Older versions of Deno used `package.json` as the configuration file, just like Node.

So, for backward compatibility it can work.
{% endhint %}

{% hint style="warning" %}
If both `deno.json` and `package.json` are present Deno will understand dependencies specified in both.

Use `deno.json` for Deno-specific configurations.

Also check more info in [#reloading-modules](#reloading-modules "mention").
{% endhint %}

### deno.json

Check a full example of the configuration file [here](https://docs.deno.com/runtime/fundamentals/configuration/#full-example).

#### [Dependencies](https://docs.deno.com/runtime/fundamentals/configuration/#dependencies)

The `"imports"` field in your `deno.json` allows you to specify dependencies used in your project.

You can use it to map bare specifiers to URLs or file paths making it easier to manage dependencies and module resolution in your applications.

#### [Tasks](https://docs.deno.com/runtime/fundamentals/configuration/#tasks) <a href="#tasks" id="tasks"></a>

The `"tasks"` field in your `deno.json` file is used to define custom commands that can be executed with the `deno task` command and allows you to tailor commands and permissions to the specific needs of your project. *(Just like* `"scripts"` *from Node)*

#### [Linting](https://docs.deno.com/runtime/fundamentals/configuration/#linting)

The `"lint"` field in the `deno.json` file is used to configure the behavior of Deno’s built-in linter.

This allows you to specify which files to include or exclude from linting, as well as customize the linting rules to suit your project’s needs.

#### [Formatting](https://docs.deno.com/runtime/fundamentals/configuration/#formatting)

The `"fmt"` field in the `deno.json` file is used to configure the behavior of Deno’s built-in code formatter.

This allows you to customize how your code is formatted, ensuring consistency across your project, making it easier to read and collaborate on.

{% hint style="info" %}
Read more about Deno default [Linting and Formatting](https://docs.deno.com/runtime/fundamentals/linting_and_formatting/).

Say goodbye to `Prettier`.
{% endhint %}

#### [Lockfile](https://docs.deno.com/runtime/fundamentals/configuration/#lockfile)

The `"lock"` field in the `deno.json` file is used to specify configuration of the lock file that Deno uses to ensure the integrity of your dependencies.

You can disable Deno from creating the `deno.lock` file with:

{% code title="deno.json" %}

```json
{
    "lock": false
}
```

{% endcode %}

Since Deno by default always add new added dependencies to the lockfile, it is also possible to freeze this lockfile *(ex. CI pipelines or production environments)* so that Deno will error when it encounter dependencies it's never seen before.

To do this you can pass the `--frozen` flag or add the configuration to `deno.json`.

{% code title="" %}

```json
{
    "lock": {
        "frozen": true
    }
}
```

{% endcode %}

#### [Typescript compiler option](https://docs.deno.com/runtime/fundamentals/configuration/#typescript-compiler-options)

The `"compilerOptions"` field in the `deno.json` file is used to configure [TypeScript compiler settings](https://www.typescriptlang.org/tsconfig) for your Deno project.

#### [Include and Exclude properties](https://docs.deno.com/runtime/fundamentals/configuration/#include-and-exclude)

Many of the configurations in `deno.json` can accept `include` and `exclude` properties.&#x20;

Check more detailed info on the docs.<br>

## Node and Npm Support

### Node.js

Deno provides a compatibility layer that allows the use of Node.js built-in APIs within Deno programs.

However, in order to use them, you will need to add the `node:` specifier to any import statements that use them.

And run it with `deno run main.mjs`.

```typescript
import * as os from "node:os";
console.log(os.cpus());
```

### Npm

Deno has native support for importing npm packages by using `npm:` specifiers. For example:

```typescript
import * as emoji from "npm:node-emoji";
console.log(emoji.emojify(`:sauropod: :heart:  npm`));
```

No `npm install` is necessary before the `deno run` command and no `node_modules` folder is created.

#### Importing types

Many npm packages ship with types, you can import these and use them with types directly:

```typescript
import chalk from "npm:chalk@5";
```

### [Node to Deno Cheatcheat](https://docs.deno.com/runtime/fundamentals/node/#node-to-deno-cheatsheet)

| Node.js                                | Deno                          |
| -------------------------------------- | ----------------------------- |
| `node file.js`                         | `deno file.js`                |
| `ts-node file.ts`                      | `deno file.ts`                |
| `nodemon`                              | `deno run --watch`            |
| `node -e`                              | `deno eval`                   |
| `npm i` / `npm install`                | `deno install`                |
| `npm install -g`                       | `deno install -g`             |
| `npm run`                              | `deno task`                   |
| `eslint`                               | `deno lint`                   |
| `prettier`                             | `deno fmt`                    |
| `package.json`                         | `deno.json` or `package.json` |
| `tsc`                                  | `deno check`                  |
| `typedoc`                              | `deno doc`                    |
| `jest` / `ava` / `mocha` / `tap` / etc | `deno test`                   |
| `nexe` / `pkg`                         | `deno compile`                |
| `npm explain`                          | `deno info`                   |
| `nvm` / `n` / `fnm`                    | `deno upgrade`                |
| `tsserver`                             | `deno lsp`                    |
| `nyc` / `c8` / `istanbul`              | `deno coverage`               |
| benchmarks                             | `deno bench`                  |

## Examples

Bellow some examples on doing things with Deno.

| Task                                                                                                                              |
| --------------------------------------------------------------------------------------------------------------------------------- |
| **Files**                                                                                                                         |
| [Reading a text file (`Deno.readTextFile`)](https://docs.deno.com/examples/reading_files/)                                        |
| [Writing a text file (`Deno.writeTextFile`)](https://docs.deno.com/examples/writing_files/)                                       |
| [Reading files in streams](https://docs.deno.com/examples/file_server_tutorial/)                                                  |
| **Network**                                                                                                                       |
| [Connect to the hostname and port (`Deno.connect`)](https://docs.deno.com/api/deno/~/Deno.connect)                                |
| [Announcing on the local transport address (`Deno.listen`)](https://docs.deno.com/api/deno/~/Deno.listen)                         |
| **Subprocess**                                                                                                                    |
| [Creating a subprocess (`Deno.Command`)](https://docs.deno.com/runtime/tutorials/subprocess/)                                     |
| **Error Handling**                                                                                                                |
| [Deno error classes](https://docs.deno.com/runtime/reference/deno_namespace_apis/#errors)                                         |
| **Permissions**                                                                                                                   |
| [Check at TS level if a permission was given](https://docs.deno.com/runtime/reference/deno_namespace_apis/#query-permissions)     |
| [Requesting permissions to User via CLI prompt](https://docs.deno.com/runtime/reference/deno_namespace_apis/#request-permissions) |
| [Revoking permissions at runtime](https://docs.deno.com/runtime/reference/deno_namespace_apis/#revoke-permissions)                |
| **Storage**                                                                                                                       |
| [Using LocalStorage in Deno](https://docs.deno.com/runtime/reference/web_platform_apis/#web-storage)                              |
| [Using WebWorkers in Deno](https://docs.deno.com/runtime/reference/web_platform_apis/#web-workers)                                |
| **NodeJs Modules**                                                                                                                |
| [Using node:crypto module](https://docs.deno.com/runtime/reference/node_apis/#node%3Acrypto)                                      |
| [Using node:os module](https://docs.deno.com/runtime/reference/node_apis/#node%3Aos)                                              |
| **Continuos Integration (CI)**                                                                                                    |
| [Setting up a basic pipeline](https://docs.deno.com/runtime/reference/continuous_integration/)                                    |
