question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NodeJS API: Changes to .js config not being picked up due to NodeJS module caching

See original GitHub issue

Environments:

  • Prettier Version: 1.14.1 and above
  • Running Prettier via: Node.js API
  • Runtime: Any Node.js version
  • Operating System: Any OS

Steps to reproduce: This is reproducible via any Prettier editor integration, e.g. prettier-vscode or Intellij prettier plugin:

  • Create a .prettierrc.js config file with, for example, a specified tab size:
module.exports = {
  tabWidth: 7
}
  • Invoke ‘Reformat Code’ from the editor. File is reformatted according to .prettierrc correctly
  • Modify .prettierrc.js, for example, changing tabWidth to 2
  • Make sure the editor persists changes to the config file to disk
  • Invoke ‘Reformat Code’ again

Expected behavior: File is reformatted with the new tabWidth (2). Actual behavior: File is reformatted with the previous tabWidth (7), until the editor is restarted. After that the file is reformatted with new settings.

With prettier version 1.13.7 the changes to the file are picked up correctly. Same with .json or .yaml config files - changes are picked up immediately after changing the config file.

This seems to be caused by the fact that Node.js require caches the loaded modules for the duration of the process. Editor integrations usually create a long-running process that then invokes ‘reformat’ multiple times, so if the config is read via require, subsequent attempts to read it will return the same result. Prettier updated cosmiconfig to version 5 here, in 1.14.1. Cosmiconfig replaced require-from-string with plain require here. require-from-string had the side effect of not caching the loaded modules. ESLint had a similar issue in the past: https://github.com/eslint/eslint/issues/5067 and addressed it by using require-uncached.

Even though this mostly affects editor integrations, I believe this should be addressed in prettier itself (or possibly in cosmiconfig), and not in editor integrations because:

  • prettier picks up changes correctly for *.json or *.yaml files
  • every editor integration would have to work around this issue, instead of only once.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
wuweiweiwucommented, Nov 27, 2018

@ikatyang sounds good! thanks ill have a PR out tonight

0reactions
wuweiweiwucommented, Nov 27, 2018

Created the PR. but the test that I created seem to fail still. Seems cosmic config 5.0.7 update doesn’t work for us. Perhaps I am missing something

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js require() cache - possible to invalidate? - Stack Overflow
Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
cache is not modified, multiple calls to require('foo') will not cause the module code to be executed multiple times. This is an important...
Read more >
Caching in Node.js to optimize app performance
Caching is a technique used in web development to handle performance bottlenecks related to how data is managed, stored, and retrieved. A cache...
Read more >
Caching In Node.js Applications - Honeybadger Developer Blog
Caching is a common technique for making your applications faster. It lets you avoid slow operations by reusing previous results.
Read more >
How To Implement Caching in Node.js Using Redis
To get around these problems, you can cache your data so that the application makes a single request to an API, and all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found