1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00
denoland-deno/docs/examples/manage_dependencies.md

74 lines
2.3 KiB
Markdown
Raw Normal View History

# Managing dependencies
2020-08-06 11:35:08 -04:00
2020-09-12 08:03:18 -04:00
## Concepts
- Deno uses URLs for dependency management.
2020-09-12 08:03:18 -04:00
- One convention places all these dependent URLs into a local `deps.ts` file.
Functionality is then exported out of `deps.ts` for use by local modules.
- Continuing this convention, dev only dependencies can be kept in a
`dev_deps.ts` file.
- See also [Linking to external code](../linking_to_external_code.md)
## Overview
2020-08-06 11:35:08 -04:00
In Deno there is no concept of a package manager as external modules are
imported directly into local modules. This raises the question of how to manage
remote dependencies without a package manager. In big projects with many
dependencies it will become cumbersome and time consuming to update modules if
they are all imported individually into individual modules.
The standard practice for solving this problem in Deno is to create a `deps.ts`
file. All required remote dependencies are referenced in this file and the
required methods and classes are re-exported. The dependent local modules then
reference the `deps.ts` rather than the remote dependencies. If now for example
one remote dependency is used in several files, upgrading to a new version of
this remote dependency is much simpler as this can be done just within
`deps.ts`.
2020-08-06 11:35:08 -04:00
2020-09-12 08:03:18 -04:00
With all dependencies centralized in `deps.ts`, managing these becomes easier.
Dev dependencies can also be managed in a separate `dev_deps.ts` file, allowing
clean separation between dev only and production dependencies.
2020-08-06 11:35:08 -04:00
2020-09-12 08:03:18 -04:00
## Example
2020-08-06 11:35:08 -04:00
```ts
/**
* deps.ts
*
2020-09-12 08:03:18 -04:00
* This module re-exports the required methods from the dependant remote Ramda module.
2020-08-06 11:35:08 -04:00
**/
export {
add,
multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js";
```
In this example the same functionality is created as is the case in the
[local and remote import examples](./import_export.md). But in this case instead
of the Ramda module being referenced directly it is referenced by proxy using a
local `deps.ts` module.
2020-09-12 08:03:18 -04:00
**Command:** `deno run example.ts`
2020-08-06 11:35:08 -04:00
```ts
2020-09-12 08:03:18 -04:00
/**
* example.ts
*/
import { add, multiply } from "./deps.ts";
2020-08-06 11:35:08 -04:00
function totalCost(outbound: number, inbound: number, tax: number): number {
return multiply(add(outbound, inbound), tax);
}
console.log(totalCost(19, 31, 1.2));
console.log(totalCost(45, 27, 1.15));
/**
* Output
*
* 60
* 82.8
*/
```