Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required json file is not removed from cache, even after a re-install #3326

Closed
serkanserttop opened this issue Oct 12, 2015 · 1 comment
Closed
Labels
question Issues that look for answers.

Comments

@serkanserttop
Copy link

We have this piece of code in our CLI tool:

  function getVersion() {
    return require('../../package.json').version;
  }

A customer experienced an issue where he re-installed a new version of our software via npm, and when he ran it, he saw that he was getting the package version from the older installation.
I can verify from his terminal log that the newer package was installed.
If it makes any difference, it is a globally installed tool.

I can have a workaround by manually deleting it from the cache.

  function getVersion() {
    //fixing Node bug
    var name = require.resolve('../../package.json');
    delete require.cache[name];
    return require('../../package.json').version;
  }

Is this a bug, or an expected behavior?
This was experienced in Node v4.1.2.
I don't know if it affects other versions.

@micnic micnic added the question Issues that look for answers. label Oct 12, 2015
@micnic
Copy link
Contributor

micnic commented Oct 12, 2015

This is the expected behavior, NOT a bug, if the json file was required at least once before the file was modified in the file system it will use the memcached version next times. In your workaround you are just updating the cache, another solution would be to request the file directly with the fs module and parse it or manage it as you want:

function getVersion() {
    return JSON.parse(fs.readFileSync('/path/to/file.json', 'utf8')).version;
}

@micnic micnic closed this as completed Oct 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

2 participants