# Modules & Dependencies

## [Modules and Dependencies](https://docs.deno.com/runtime/fundamentals/modules/)

### Reloading Modules

By default, Deno uses a global cache directory (`DENO_DIR`) for downloaded dependencies. This cache is shared across all projects.

{% hint style="danger" %}
Have this in mind when starting projects:

* If project only have `deno.json` file, `node_modules` folder will NOT be created and dependencies will be downloaded to this global cache.
  * *It may be possible to force* `deno.json` *to create* `node_modules` *check more at docs.*
* If project have `deno.json` and `package.json` files, `node_modules` is created and dependencies are downloaded to it.
  {% endhint %}

You can force deno to refetch and recompile modules into the cache using the `--reload` flag.

```bash
# Reload everything
deno run --reload my_module.ts

# Reload a specific module
deno run --reload=jsr:@std/fs my_module.ts
```

### Third party packages

Can be imported with `jsr`, `npm` and (`https` and `http`).

#### Directly in the `.ts` files

```typescript
import { camelCase } from "jsr:@luca/cases@1.0.0";
import { say } from "npm:cowsay@1.6.0";
import { pascalCase } from "https://deno.land/x/case/mod.ts";
```

{% hint style="info" %}
Deno recommends [JSR](https://jsr.io/), the modern JavaScript registry, for third party modules. There, you'll find plenty of well documented ES modules for your projects, including the [Deno Standard Library](https://docs.deno.com/runtime/fundamentals/standard_library/).
{% endhint %}

#### In `deno.json`

To better maintain code you can also map these imports in the `"imports"` section of `deno.json`.

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

```json
{
    "imports": {
        "@luca/cases": "jsr:@luca/cases@^1.0.0",
        "cowsay": "npm:cowsay@^1.6.0",
        "cases": "https://deno.land/x/case/mod.ts"
    }
}
```

{% endcode %}

```typescript
import { camelCase } from "@luca/cases";
import { say } from "cowsay";
import { pascalCase } from "cases";
```

#### With `deno add`

An even better way is to use the CLI to import these packages. It will automatically add them to the `deno.json` as `"imports"` and get the latest versions.

```bash
deno add jsr:@luca/cases
```

### [Package Version Managing](https://docs.deno.com/runtime/fundamentals/modules/#package-versions)

It is possible to be specific in what versions of packages you may want to import.

```
@scopename/mypackage           # highest version
@scopename/mypackage@16.1.0    # exact version
@scopename/mypackage@16        # highest 16.x version >= 16.0.0
@scopename/mypackage@^16.1.0   # highest 16.x version >= 16.1.0
@scopename/mypackage@~16.1.0   # highest 16.1.x version >= 16.1.0
```

### [Private Repositories](https://docs.deno.com/runtime/fundamentals/modules/#private-repositories)

Deno can also load modules from private repositories like on Github.

Deno supports sending bearer tokens when requesting a remote module.

Check more info in the docs.

## [Standard Library](https://docs.deno.com/runtime/fundamentals/standard_library/)

Deno provides a standard library written in TypeScript.

This standard library provide some packages for dealing with `json`, `path`, and many other.

The standard library is hosted on JSR and is available at: <https://jsr.io/@std>.

To install packages from the Deno Standard Library, you can use the `deno add` subcommand to add the package to your `deno.json` import map.

```bash
deno add jsr:@std/fs jsr:@std/path
```

```typescript
import { copy } from "@std/fs";
import { join } from "@std/path";
```
