Search code examples
javascriptnode.jscachingtemporary-files

How to keep temporary files between multiple runs in nodejs?


I have a small node module which generates files.

As it is really slow and will produce the same result for the same input I would like to keep the last compilation together with a control hash.

The question is now where do I have to place the temporary files so it can be easily accessed?

The cache should also work if the main node application which depends on my module restarts.


Solution

  • I'll collect all my comments into an answer.

    As far as I know, there is no NPM standard for where a module would put it's tempfiles. The best place to put them can depend upon how file permissions are configured, what operating system you're running, what permissions the host app is running under, the type of hosting environment, etc...

    The logical options are as follows:

    1. In the OS temp directory
    2. In a temp sub-directory below the module directory.
    3. In a configurable directory that the user of the module can specify either via a config argument or via an environment variable.

    You can find out where the OS temp directory is with os.tmpdir().

    A temp sub-directory below the module. Keep in mind that there can be multiple processes using a module so if you're putting files in a location that may be shared by multiple processes, then you need to be using generated unique names if the files are supposed to be separate per process or appropriate locking if the files are supposed to be shared among processes.

    And, don't forget about cleanup maintenance so there's no build-up of temporary files over time.