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

bug: fails to load config in ESM project #60

Closed
elijaholmos opened this issue Nov 15, 2022 · 11 comments
Closed

bug: fails to load config in ESM project #60

elijaholmos opened this issue Nov 15, 2022 · 11 comments
Labels

Comments

@elijaholmos
Copy link

In any project where "type": "module" exists in the package.json, this package crashes when trying to load a config file. The error that appears is similar to:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\coding-work\cosmiconfig\cosmiconfigts-repro\amazing.config.ts
require() of ES modules is not supported.
require() of C:\coding-work\cosmiconfig\cosmiconfigts-repro\amazing.config.ts from C:\coding-work\cosmiconfig\cosmiconfigts-repro\node_modules\.pnpm\cosmiconfig-typescript-loader@4.2.0_whq6te2zcgakt4fgsicma7l33e\node_modules\cosmiconfig-typescript-loader\dist\cjs\index.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from C:\coding-work\cosmiconfig\cosmiconfigts-repro\package.json.

The cause of the issue appears to be with the require statement here:

// cosmiconfig requires the transpiled configuration to be CJS
tsNodeInstance.compile(content, path);
const result = require(path);

I can provide a sample repro repo if requested.

related issue: dotansimha/graphql-code-generator-community#225

@Codex-
Copy link
Owner

Codex- commented Nov 16, 2022

I'm very keen to support import and remove the cjs limitation, however this requires that cosmiconfig supports esm too. Currently they only emit cjs: https://github.com/davidtheclark/cosmiconfig/blob/main/package.json#L95

@geisterfurz007
Copy link

For visibility, this is the related upstream PR cosmiconfig/cosmiconfig#283

@tal-rofe
Copy link

Is there any workaround?

@Codex-
Copy link
Owner

Codex- commented Apr 14, 2023

Is there any workaround?

For not being able to load esm? Can't do much about this until cosmiconfig supports this upstream

@tianyingchun
Copy link

i just create temp module @armit/config-loader to support esm,cjs, type:module

@elijaholmos
Copy link
Author

with cosmiconfig 8.2.0, ESM is now supported! 🎉

@tianyingchun
Copy link

tianyingchun commented Jun 5, 2023

it seems that their is a bug.

const loadJs: LoaderAsync = async function loadJs(filepath) {
  try {
    const { href } = pathToFileURL(filepath);
    return (await import(href)).default;
  } catch (error) {
    return loadJsSync(filepath, null);
  }
};

return (await import(href)).default; always return undefined for project

type:module

it should be changed to

const { href } = pathToFileURL(filepath);
const moduleData =  await import(href);
return moduleData.default.|| moduleData;

@tianyingchun
Copy link

tianyingchun commented Jun 5, 2023

consider below example
valid.config.js

import { defineConfig } from 'vite';

// it is `esm` module causeof type:`module`
export const test = defineConfig({
  cake: 'a lie',
});

it will aways return me undefined.

@Codex-
Copy link
Owner

Codex- commented Jun 5, 2023

With esm now supported I'll have to make some changes to support this, PRs welcome but I'll try look at it this week

@Codex-
Copy link
Owner

Codex- commented Jun 6, 2023

This is now in progress, draft PR here https://github.com/Codex-/cosmiconfig-typescript-loader/pull/96

@Codex-
Copy link
Owner

Codex- commented Jun 6, 2023

Progress can be tracked in #97

@Codex- Codex- closed this as completed Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants