| ```js |
| 6 | import { add } from "https://esm.sh/lodash-es"; |
| 7 | console.log(add(1, 2)); // 3 |
| 8 | ``` |
| 9 |
| 10 | ## Usage |
| 11 |
| 12 | ```sh |
| 13 | npm i -g node-url-import |
| 14 | # Or: npx node-url-import [args] |
| 15 |
| 16 | node-url-import [options] [node-flags] [script-or-url] [script-args...] |
| 17 | ``` |
| 18 |
| 19 | Put loader options first; the remaining arguments go to Node unchanged. Use `--` before filenames beginning with `-`. |
| 20 |
| 21 | ```sh |
| 22 | node-url-import ./app.mjs |
| 23 | node-url-import https://raw.esm.sh/armor64/dist/cli.js -h |
| 24 | node-url-import --cdn --inspect ./app.mjs |
| 25 | node-url-import --lock --frozen ./app.mjs |
| 26 | ``` |
| 27 |
| 28 | | Option | Environment variable | Description | |
| 29 | | --- | --- | --- | |
| 30 | | `--reload` | `URL_IMPORT_RELOAD=1` | Bypass disk cache | |
| 31 | | `--lock` | `URL_IMPORT_LOCK=<path>` | Check/write `./deno.lock` | |
| 32 | | `--lock-file <path>` | `URL_IMPORT_LOCK=<path>` | Use a custom lock path | |
| 33 | | `--frozen` | `URL_IMPORT_FROZEN=1` | Reject lock changes | |
| 34 | | `--tmp` | `URL_IMPORT_TMP=1` | Load all remote modules from readable local files; `import.meta.url` becomes a `file:` URL | |
| 35 | | `--cdn[=<url>]` | `URL_IMPORT_CDN=<base/>` | Resolve missing bare packages via a CDN (default: `https://esm.sh/`) | |
| 36 | | `--clear-cache` | — | Purge the shared remote cache | |
| 37 |
| 38 | Boolean options are flag-only: boolean values and `--no-*` forms are unsupported. Set environment variables before registration; use `0` to disable inherited booleans, or unset CDN/lock variables to disable those settings. |
| 39 |
| 40 | For private modules, `DENO_AUTH_TOKENS` accepts `token@host` for Bearer auth or `user:password@host` for Basic auth, with `;` separating entries. Domain entries also match subdomains; IP entries match only that address. Include a non-default port in `host` when needed. Each redirect target uses the first matching entry in configuration order. |
| 41 |
| 42 | Register without the CLI: |
| 43 |
| 44 | ```sh |
| 45 | node --import node-url-import/register ./app.mjs |
| 46 | node --import node-url-import/register ./app.cjs |
| 47 | node --loader node-url-import/loader ./app.mjs # Legacy, ESM only |
| 48 | ``` |
| 49 |
| 50 | Register programmatically (Node 22.15+): |
| 51 |
| 52 | ```js |
| 53 | import { createResolve, createLoad, fetchModuleSync } from "node-url-import"; |
| 54 | import { registerHooks } from "node:module"; |
| 55 | registerHooks({ |
| 56 | resolve: createResolve(fetchModuleSync), |
| 57 | load: createLoad(fetchModuleSync), |
| 58 | }); |
| 59 | ``` |
| 60 |
| 61 | | Feature | Minimum Node.js | |
| 62 | | --- | --- | |
| 63 | | ESM imports | 18.19 / 20.6 | |
| 64 | | Remote TypeScript | 22.18 | |
| 65 | | CJS `require()` of HTTP URLs | 24 | |
| 66 |
| 67 | ## Imports |
| 68 |
| 69 | ### `npm:` |
| 70 |
| 71 | Local packages use `require.resolve`, ignoring the requested version. Missing packages fall back to the configured CDN (or esm.sh), preserving version and subpath. Export errors do not trigger fallback. Remote modules look up local packages from the working directory. |
| 72 |
| 73 | ```js |
| 74 | import ms from "npm:ms@2.1.3"; |
| 75 | import add from "npm:lodash-es@4/add"; |
| 76 | import { parse } from "npm:@babel/parser@7"; |
| 77 | ``` |
| 78 |
| 79 | ### CDN, `jsr:`, and JSON |
| 80 |
| 81 | `--cdn` checks `node_modules` first and resolves missing bare packages from a CDN. Custom HTTP(S) bases must omit query/fragment; a trailing `/` is added. Built-ins, paths, full URLs, and `#imports` are not CDN-mapped. `jsr:` requires an esm.sh base; ESM JSON requires `type: "json"`. |
| 82 |
| 83 | ```js |
| 84 | import { z } from "zod"; // --cdn → local zod, else https://esm.sh/zod |
| 85 | import { encodeBase64 } from "jsr:@std/encoding@1.0.0/base64"; |
| 86 | import data from "https://example.com/data.json" with { type: "json" }; |
| 87 | ``` |
| 88 |
| 89 | ## Cache and lock |
| 90 |
| 91 | Shares **Deno 2.9.8** remote-cache and v5 lock formats only; older layouts are not read or migrated. Node's in-process module cache still applies. |
| 92 |
| 93 | Cache keys include queries but exclude fragments; full URLs identify modules and lock records. Redirects use Deno's URL-join rules: `Location` without `#` drops the request fragment. Relative imports use the final URL, with up to 10 redirect hops. |
| 94 |
| 95 | Enable locking with `--lock` or a configured lock path. Hash mismatches always fail; `--frozen` rejects missing/changed records. `--reload` neither overwrites hashes nor repins redirects. Unrelated Deno lock sections are preserved. |
| 96 |
| 97 | Explicit `--frozen` supplies `./deno.lock` if no path is configured. `URL_IMPORT_FROZEN=1` alone does not enable locking. |
| 98 |
| 99 | ## Programmatic API |
| 100 |
| 101 | Caller-provided headers override defaults. Explicit `Authorization` overrides `DENO_AUTH_TOKENS`; cross-origin redirects strip `Authorization`, `Proxy-Authorization`, `Cookie` and `Cookie2`, then match tokens for the destination. |
| 102 |
| 103 | `fetchModule` and `fetchModuleSync` accept the same options: `timeoutMs` sets the request timeout, and `headers` supplies request headers. |
| 104 |
| 105 | ```js |
| 106 | import { fetchModule, fetchModuleSync, clearCache } from "node-url-import"; |
| 107 |
| 108 | const url = "https://esm.sh/lodash-es/add"; |
| 109 | const options = { |
| 110 | timeoutMs: 10_000, |
| 111 | headers: { "User-Agent": "my-app" }, |
| 112 | }; |
| 113 |
| 114 | const { source, contentType, finalUrl, redirectChain } = await fetchModule(url, options); |
| 115 | console.log(source); |
| 116 | const cached = fetchModuleSync(url, options); // Synchronously reuses the cached response |
| 117 | clearCache(); // Synchronously purges the shared remote cache |
| 118 | ``` |
| 119 |
| 120 | ## Limitations |
| 121 |
| 122 | - TypeScript uses native type stripping; `.cts` retains CommonJS semantics. |
| 123 | - Bundles calling `createRequire(import.meta.url)` fail on HTTP URLs. Use `--tmp`, a compatible CDN build, or a local package. Temporary files remain until manually removed or cleaned by the OS. |
| 124 | - With synchronous hooks (`module.registerHooks()`, Node 22.15+), remote CommonJS modules (`.cjs` and supported `.cts`) are written to temporary files automatically, even without `--tmp`, so ordinary relative `require()` calls work through Node's native CommonJS loader. `require.resolve()` uses Node's internal resolver without invoking hooks, so it cannot resolve remote specifiers. `__filename`, `module.filename` and `require.cache` use temporary file paths, preserving CommonJS file identity. The async `module.register()` loader does not provide this automatic CommonJS materialization; Node 24+ is required for `require()` of HTTP URLs. |
| 125 | - For remote CLI entries, `process.argv[1]` is path-normalized; use `import.meta.url` for the module location. |
| 126 |
| 127 | TypeScript needs ambient declarations for URL imports. Add them to an included `.d.ts` file, for example: |
| 128 |
| 129 | ```ts |
| 130 | declare module "https://esm.sh/lodash-es" { |
| 131 | export { add, merge } from "lodash-es"; |
| 132 | } |
| 133 | ``` |
| 134 |