4

I want to clear Node's cache from my graphql defintions but nothing happens until I make a change to the file where the require('my.graphql') statement is. I think this question relates to this one and I've tried to do:

Object.keys(require.cache).forEach(function(key) {
  delete require.cache[key];
});

But it doesn't reload the code. There must be some other caching that is going on that I miss.

Since the example would be a little long, I've forked this repo and created a graphql-branch that you can install and test adapting the src/schema.graphql file:

git clone git@github.com:gforge/graphql-relay-mongodb-pagination.git
cd graphql-relay-mongodb-pagination
git checkout git checkout mongoose-w-gql-lang 
npm install

The require('my.graphql') is mediated via babel-plugin-inline-import.

Community
  • 1
  • 1
Max Gordon
  • 5,367
  • 2
  • 44
  • 70

3 Answers3

5

So it turns out that the problem is actually Babel's cache and not Node's. Changing the start in package.json:

...
"start": "babel-node ./src/index.js",

to

...
"start": "BABEL_DISABLE_CACHE=1 babel-node ./src/index.js",

fixed the issue.

Max Gordon
  • 5,367
  • 2
  • 44
  • 70
3

The babel docs suggest

BABEL_CACHE_PATH=/foo/my-cache.json babel-node script.js  # default in /tmp/
BABEL_DISABLE_CACHE=1 babel-node script.js
require('babel-register')({ cache: false });

But this doesn't work on Windows 7 when debugging in IntelliJ. After much googling I found where it really lives

C:\Users\username\.babel.json
%USERPROFILE%\.babel.json  
$USERPROFILE/.babel.json       # cygwin

You can add a script to your package.json to do this for you

{
  "scripts": {
    "clean": "del %USERPROFILE%/.babel.json",
    "clean": "bash -c 'rm -vf $USERPROFILE/.babel.json'"
  }
}

If you are using webpack you can dynamically inject a cacheDirectory: parameter into .babelrc file for easier deletion

babel-node throws an error if you add it directly to the file

webpack.config.js

const _       = require('lodash');
const JSON5   = require('json5');

const babelrc = _.extend(
    // WARNING:  ./.babel-cache/ may occasionally get corrupted and need "npm run clean"
    // POSITIVE: ./.babel-cache/ provides a ~30% speed increase in recompile times
    { cacheDirectory: !argv.production && './.babel-cache' },
    JSON5.parse(fs.readFileSync('./.babelrc'))
);
James McGuigan
  • 7,542
  • 4
  • 26
  • 29
0

Instead of disabling you may prefer to clear the cache:

$ rm -rf ./node_modules/.cache
Davi Lima
  • 800
  • 1
  • 6
  • 20