Search code examples
node.jsbashcachingts-nodets-node-dev

package.json ts-node-dev parameter: --cache-directory and TS_NODE_CACHE_DIRECTORY


I am using ts-node-dev and I need to change its cache directory (defaults to the operating system's temporary directory).

I have two commands defined in a package.json file, a start and a start-custom-cache like so:

"scripts": {
    "start-custom-cache": "ts-node-dev --transpile-only --cache-directory $npm_config_myflag_cache_dir ./src/parser.ts",
    "start": "ts-node-dev --transpile-only ./src/parser.ts",
     ...
}

I can launch start from a bash script like this:

npm start --prefix parser -- -f "$FILEPATH" -c "$CONFIGPATH" -o "$NORMALIZED" -g "$GRAPH_DIR" --csv

This works correctly and those variables get passed as arguments correctly. The problem is that I am defining a desirable cache directory (rather than the default operating system temporary directory) and want to pass it to ts-node-dev.

According to this source there is the environment variable TS_NODE_CACHE_DIRECTORY I can set to change it. Checking the npm package information here, it should apparently match a flag --cache-directory. I've tried running the following but it still writes to /tmp/:

export TS_NODE_CACHE_DIRECTORY="$NPM_CACHE_DIR"
npm start --prefix parser -- -f "$FILEPATH" -c "$CONFIGPATH" -o "$NORMALIZED" -g "$GRAPH_DIR" --csv

There appears to be a --cache-directory parameter to be passed. I've tried passing it like this but it didn't work:

npm run start --prefix parser -- -f "$FILEPATH" -c "$CONFIGPATH" -o "$NORMALIZED" -g "$GRAPH_DIR" --csv --cache-directory "$NPM_CACHE_DIR"

Based on this source I also tried this (with the equals sign):

npm run start --prefix parser -- -f "$FILEPATH" -c "$CONFIGPATH" -o "$NORMALIZED" -g "$GRAPH_DIR" --csv --cache-directory="$NPM_CACHE_DIR"

Lastly, following suggestions from this source, I also tried using the start-custom-cache variant like so:

npm run start-custom-cache --prefix parser --myflag_cache_dir="$NPM_CACHE_DIR" -- -f "$FILEPATH" -c "$CONFIGPATH" -o "$NORMALIZED" -g "$GRAPH_DIR" --csv

Unfortunately, nothing works.

Thanks for reading, would be amazing if someone could chime in.


Solution

  • It seems that the behavior I was trying to use has been deprecated:

    https://github.com/npm/rfcs/blob/main/implemented/0021-reduce-lifecycle-script-environment.md

    In versions of npm up to v6, the following items are all placed into the environment of scripts run for various lifecycle events (install, prepare, etc., as well as explicit scripts such as test and start).

    PATH Configured to include .../node_modules/.bin for current and all parent node_modules directories.
    npm_package_* for all package.json values in the current package for which the lifecycle event is running.
    npm_config_* for all npm configuration values that do not start with a _ character.
    npm_lifecycle_event the current lifecycle event.
    npm_lifecycle_script the command being run.
    npm_node_execpath the path to the Node.js executable npm is using.
    npm_execpath the path to the npm executable being run.
    

    The suggestion presented here is to remove (or vastly reduce) the npm_config_X and npm_package_X environment variables from the context of lifecycle scripts, and potentially also add new fields that may be more useful to more users.

    Summarizing, defining variables in a config section in the package.json to then use them in the scripts section does not seem to work for almost all possible values. This means that defining these configuration variables as parameters from the command line no longer works.

    In my case, the solution I found was to export the Linux temporary directory environment variables $TMP, $TEMP and $TMPDIR in my bash script before invoking npx and then reset them after the command finishes. This way, intermediate files are written in the directory I designate and it is safe to delete the directory after.