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

CJS configuration files in ESM projects do not freshly import #344

Open
spence-s opened this issue Mar 1, 2024 · 0 comments
Open

CJS configuration files in ESM projects do not freshly import #344

spence-s opened this issue Mar 1, 2024 · 0 comments

Comments

@spence-s
Copy link

spence-s commented Mar 1, 2024

I ran into a situation where if a configuration file is a cjs file, then cosmiconfig will always return the same configuration files between runs even if it has changed:

consider a quick psuedo example:

const getConfig = async () => {
  const configExplorer = cosmiconfig(MODULE_NAME);

  const config = await configExplorer.search('...');
  
  return config
}

// see the old config
const oldConfig = await getConfig()

// change the config somehow
changeConfigSomehow();

// should see an updated config here
const newConfig = await getConfig()

This works for JSON file types, but CJS file type in and ESM project will fail.

My hunch is that it is because it gets cached in the internal ESM cache which cannot be busted like the old require cache. see nodejs/node#49442

Internally, cosmiconfig uses a package called import-fresh which can only do fresh imports for the CJS cache, in an ESM context, I don't believe it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant