Deno: A way to require something without caching it.

Created on 2 Aug 2018  路  12Comments  路  Source: denoland/deno

NodeJS doesn't have this feature. :trollface:

All 12 comments

@Kompwu node is stuck in CommonJS land and is struggling to move to ESM.

Deno already uses ESM (es modules) - I think it should have semantics as clear as possible and compatible with the JavaScript language and browsers

module.js

console.log("module");

index.js

require("./module");
require("./module");

// prints "module" once

function requireUncached(module) {
  delete require.cache[require.resolve(module)];
  return require(module);
}

requireUncached("./module");
requireUncached("./module");

// prints "module" twice

If you want to require module without caching just delete it from cache after require finishes like:

function requireUncached(module) {
  delete require.cache[require.resolve(module)];
  const required = require(module);
  delete require.cache[require.resolve(module)];
  return required;
}

@elderapo Why there is two time delete require.cache[require.resolve(module)]; ? 馃

@Kompwu because without second delete statement it doesn't really require module without caching =).

First delete statement takes care of scenario where you require module using normal require function before using requireUncached. For example:

// if you delete first delete statement
require('./module'); // prints "module"
requireUncached('./module'); // doesn't print "module" because previous "require" statement cached "./module"
requireUncached('./module'); // prints "module"

Second delete statement +/- does what you asked for. It clears cache after requring module ~ it requires module without caching it. It's not exactly what you asked for but it's as close as it can get (I think) without changing implementation of require function in NodeJS sources.

// if you delete second delete statement
requireUncached('./module'); // prints "module"
require('./module'); // doesn't print "module" because previous "requireUncached" didn't remove module from cache after requiring it

What about using
deno index.ts --no-cache
That implements something like we do in python?

CC @kitsonk can you give your opinion?

Why would someone want this?

Won't implement.

To skip the cache, you can do:

const { default: page } = await import(`file:///file.ts?${+new Date()}.ts`;);

And here a full example:

import { writeFileStr } from "https://deno.land/std/fs/write_file_str.ts";
async function test(n: number) {
  const file = "./file.ts";
  await writeFileStr(
    file,
    `console.log(${n});`
  );
  await import(`${file}?${+new Date()}${Math.random()}.ts`);
}
async function main() {
  await test(1);
  await test(2);
  await test(3);
}
main();

But this hack doesn't always work. Also if you have some import in file.ts, then those import will use the cache. I have lot of issues with my tool to implement Hot Module Reloading https://github.com/apiel/adka/blob/master/generatePages/generatePages.ts#L32

I will try another approach, that will be to copy all the source code I want to dynamically import without cache, inside another temporary folder (everytime a different folder) therefor deno should not be able to use cache.

I understand that deno want to be as much as possible browser compatible but in another way deno run on the server and not everything can work exactly the same. So I wonder if at some point, we should not offer some solution for tools that are not intended to run in browser. With hacking around, we will find solution but would be great to have simple way to do things instead to hack...

@apiel can't you implement a loader? That is the standards compliant way afaik

@benjamingr what do you mean by a loader? Do you mean something like requirejs or systemjs. If yes, why to build a loader, when there is already one implemented in Deno. I need to do hot module reloading server side and not in browser. Building a loader, would force me to first transpile the code before to build it, or the good thing with Deno, is that they take care of bundling and compiling the code for us... I would like to use Deno as much as possible and not have to implement something like babel...

@apiel no, I mean a loader, I don't think deno supports these yet but they enable hooks for these things and would probably allow for hmr.

Was this page helpful?
0 / 5 - 0 ratings