You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support clearing Node.js cache for package.json #54924
When a long-running Node.js process is active and a package.json file is modified (for example, by adding "type": "module"), Node.js does not detect this change. It continues treating the package as CommonJS, which results in an error like Unexpected token 'export' when attempting to use ES modules.
This is crucial for us at bit, where we run a background process called “bit server” that manages the local development workspace. Among its tasks is creating new web components, which initially have a package.json without "type": "module". If the component is later determined to be ESM, the package.json is updated accordingly. (Explaining why this can’t be handled differently is beyond the scope of this feature request).
What is the feature you are proposing to solve the problem?
I propose adding an API that allows clearing the cached information related to the package.json of a specific module (or optionally clearing the cache for all modules). Either option would solve the issue.
What alternatives have you considered?
Restarting the Node.js process, but this results in a poor user experience.
I created a simple project to demonstrate it. You can simply clone it git clone https://github.com/davidfirst/node-pkg-json-caching-issue.git and run node pkg1/index.js
I know you said it's out of scope, but I'd love to understand why adding type:module (and changing the meaning of every .js file in the package, not all of which might be the same format) is easier than changing the file extensions to .mjs for files that are ESM?
Yes I think it might work.
I thought about trying it as well.
I still have some concerns about some use cases but we can definitely try it at least.
I still think that from node.js perspective it will be a good idea to provide a way to clear that cache like you can clear the require cache.
Now that I think about it, the templates that Bit allows users to create components from can actually be developed by the community. This means we don’t have full control over these templates, and developers might configure the "type": "module" setting in the package.json instead of using file extensions. While we can document this, it’s not a foolproof solution. Identifying the cause of this issue can be quite challenging.
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the
never-staleIssues and PRs exempt from automated stale handling.
label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
Hi there, I'd like to contribute. Is this still needed? If so, I'd like to know if you're okay with package.json re-loading on a file change or would like to expose a function for this. Thank you for your time.
Activity
avivkeller commentedon Sep 13, 2024
How are you loading the files?
requireorimport? Can you provide a little example that shows this error?davidfirst commentedon Sep 13, 2024
I'm loading the files with
await import()syntax.I created a simple project to demonstrate it. You can simply clone it
git clone https://github.com/davidfirst/node-pkg-json-caching-issue.gitand runnode pkg1/index.jsavivkeller commentedon Sep 13, 2024
CC @nodejs/loaders
ljharb commentedon Sep 13, 2024
I know you said it's out of scope, but I'd love to understand why adding type:module (and changing the meaning of every
.jsfile in the package, not all of which might be the same format) is easier than changing the file extensions to.mjsfor files that are ESM?davidfirst commentedon Sep 13, 2024
@GiladShoham please take a look at the last message. Do you think this would be a viable option for Bit?
GiladShoham commentedon Sep 15, 2024
Yes I think it might work.
I thought about trying it as well.
I still have some concerns about some use cases but we can definitely try it at least.
I still think that from node.js perspective it will be a good idea to provide a way to clear that cache like you can clear the require cache.
davidfirst commentedon Sep 16, 2024
Now that I think about it, the templates that Bit allows users to create components from can actually be developed by the community. This means we don’t have full control over these templates, and developers might configure the "type": "module" setting in the package.json instead of using file extensions. While we can document this, it’s not a foolproof solution. Identifying the cause of this issue can be quite challenging.
github-actions commentedon Mar 15, 2025
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-staleIssues and PRs exempt from automated stale handling.
label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
davidfirst commentedon Mar 17, 2025
never-stale
crydafan commentedon Jan 24, 2026
Hi there, I'd like to contribute. Is this still needed? If so, I'd like to know if you're okay with
package.jsonre-loading on a file change or would like to expose a function for this. Thank you for your time.