Skip to content

Commit 8b1df4b

Browse files
authored
Add Overriding Dependencies section (#1375)
1 parent 87e5781 commit 8b1df4b

File tree

1 file changed

+98
-44
lines changed

1 file changed

+98
-44
lines changed

runtime/fundamentals/modules.md

Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,66 @@ the best experience.**
264264

265265
:::
266266

267+
## Overriding dependencies
268+
269+
Deno provides mechanisms to override dependencies, enabling developers to use
270+
custom or local versions of libraries during development or testing.
271+
272+
Note: If you need to cache and modify dependencies locally for use across
273+
builds, consider [vendoring remote modules](#vendoring-remote-modules).
274+
275+
### Overriding local JSR packages
276+
277+
For developers familiar with `npm link` in Node.js, Deno provides a similar
278+
feature for local JSR packages through the `patch` field in `deno.json`. This
279+
allows you to override dependencies with local versions during development
280+
without needing to publish them.
281+
282+
Example:
283+
284+
```json title="deno.json"
285+
{
286+
"patch": [
287+
"../some-package-or-workspace"
288+
]
289+
}
290+
```
291+
292+
Key points:
293+
294+
- The `patch` field accepts paths to directories containing JSR packages or
295+
workspaces. If you reference a single package within a workspace, the entire
296+
workspace will be included.
297+
- This feature is only respected in the workspace root. Using `patch` elsewhere
298+
will trigger warnings.
299+
- Currently, `patch` is limited to JSR packages. Attempting to patch `npm`
300+
packages will result in a warning with no effect.
301+
302+
Limitations:
303+
304+
- `npm` package overrides are not supported yet. This is planned for future
305+
updates.
306+
- Git-based dependency overrides are unavailable.
307+
- The `patch` field requires proper configuration in the workspace root.
308+
- This feature is experimental and may change based on user feedback.
309+
310+
### Overriding NPM packages
311+
312+
We plan to support NPM packages with the patch functionality described above,
313+
but until then if you have a `node_modules` directory, `npm link` can be used
314+
without change to accheive the same effect. This is typically done with
315+
`{ "nodeModulesDir": "manual" }` set in the `deno.json` file. See also the
316+
documentation on [`node_modules`](/runtime/fundamentals/node/#node_modules)
317+
267318
### Overriding HTTPS imports
268319

269-
The other situation where import maps can be very useful is to override HTTPS
270-
imports in specific modules.
320+
Deno also allows overriding HTTPS imports through the `importMap` field in
321+
`deno.json`. This feature is particularly useful when substituting a remote
322+
dependency with a local patched version for debugging or temporary fixes.
271323

272-
Let's say you want to override a `https://deno.land/x/my-library@1.0.0/mod.ts`
273-
specifier that is used inside files coming from `https://deno.land/x/example/`
274-
to a local patched version. You can do this by using a scope in the import map
275-
that looks something like this:
324+
Example:
276325

277-
```json
326+
```json title="deno.json"
278327
{
279328
"imports": {
280329
"example/": "https://deno.land/x/example/"
@@ -287,12 +336,50 @@ that looks something like this:
287336
}
288337
```
289338

290-
:::note
339+
Key points:
291340

292-
HTTPS imports have no notion of packages. Only the import map at the root of
293-
your project is used. Import maps used inside URL dependencies are ignored.
341+
- The `scopes` field in the import map allows you to redirect specific imports
342+
to alternative paths.
343+
- This is commonly used to override remote dependencies with local files for
344+
testing or development purposes.
345+
- Import maps apply only to the root of your project. Nested import maps within
346+
dependencies are ignored.
294347

295-
:::
348+
## Vendoring remote modules
349+
350+
If your project has external dependencies, you may want to store them locally to
351+
avoid downloading them from the internet every time you build your project. This
352+
is especially useful when building your project on a CI server or in a Docker
353+
container, or patching or otherwise modifying the remote dependencies.
354+
355+
Deno offers this functionality through a setting in your `deno.json` file:
356+
357+
```json
358+
{
359+
"vendor": true
360+
}
361+
```
362+
363+
Add the above snippet to your `deno.json` file and Deno will cache all
364+
dependencies locally in a `vendor` directory when the project is run, or you can
365+
optionally run the `deno install --entrypoint` command to cache the dependencies
366+
immediately:
367+
368+
```bash
369+
deno install --entrypoint main.ts
370+
```
371+
372+
You can then run the application as usual with `deno run`:
373+
374+
```bash
375+
deno run main.ts
376+
```
377+
378+
After vendoring, you can run `main.ts` without internet access by using the
379+
`--cached-only` flag, which forces Deno to use only locally available modules.
380+
381+
For more advanced overrides, such as substituting dependencies during
382+
development, see [Overriding dependencies](#overriding-dependencies).
296383

297384
## Publishing modules
298385

@@ -335,39 +422,6 @@ deno run --cached-only mod.ts
335422
This will fail if there are any dependencies in the dependency tree for mod.ts
336423
which are not yet cached.
337424

338-
## Vendoring remote modules
339-
340-
If your project has external dependencies, you may want to store them locally to
341-
avoid downloading them from the internet every time you build your project. This
342-
is especially useful when building your project on a CI server or in a Docker
343-
container, or patching or otherwise modifying the remote dependencies.
344-
345-
Deno offers this functionality through a setting in your `deno.json` file:
346-
347-
```json
348-
{
349-
"vendor": true
350-
}
351-
```
352-
353-
Add the above snippet to your `deno.json` file and Deno will cache all
354-
dependencies locally in a `vendor` directory when the project is run, or you can
355-
optionally run the `deno install --entrypoint` command to cache the dependencies
356-
immediately:
357-
358-
```bash
359-
deno install --entrypoint main.ts
360-
```
361-
362-
You can then run the application as usual with `deno run`:
363-
364-
```bash
365-
deno run main.ts
366-
```
367-
368-
After vendoring, you can run `main.ts` without internet access by using the
369-
`--cached-only` flag, which forces Deno to use only locally available modules.
370-
371425
## Integrity Checking and Lock Files
372426

373427
Imagine your module relies on a remote module located at https://some.url/a.ts.

0 commit comments

Comments
 (0)