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

Extension cannot be used it type is set to module #2342

Closed
cupcakearmy opened this issue May 31, 2021 · 3 comments · Fixed by #2381
Closed

Extension cannot be used it type is set to module #2342

cupcakearmy opened this issue May 31, 2021 · 3 comments · Fixed by #2381

Comments

@cupcakearmy
Copy link

cupcakearmy commented May 31, 2021

Intended outcome:

The extensions should work and load the local schema.

Actual outcome:

Cannot load schema.

[ERROR] A config file failed to load with options: {"configPath":"/***/***/Desktop/test","requireConfig":true}.
    The error was: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /***/***/Desktop/test/apollo.config.js
require() of ES modules is not supported.
require() of /***/***/Desktop/test/apollo.config.js from /***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/node_modules/import-fresh/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename apollo.config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /***/***/Desktop/test/package.json.

    at Object.loadConfig (/***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/node_modules/apollo-language-server/lib/config/loadConfig.js:42:34)
    at async GraphQLWorkspace.reloadProjectForConfig (/***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/node_modules/apollo-language-server/lib/workspace.js:107:22)
    at /***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/src/extension.ts:77:17
    at handleNotification (/***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/node_modules/vscode-jsonrpc/lib/main.js:489:43)
    at processMessageQueue (/***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/node_modules/vscode-jsonrpc/lib/main.js:260:17)
    at Immediate._onImmediate (/***/***/.vscode/extensions/apollographql.vscode-apollo-1.19.3/node_modules/vscode-jsonrpc/lib/main.js:247:13)

This is because in the package.json there is "type": "module" set. Node then treats files as modules and not commonjs. This means that module.exports does not work.

How to reproduce the issue:

  1. Create a package.json. (See below)
  2. Add a config file apollo.config.js with an endpoint (See below)
  3. Reload window and reload schema
{
  "private": true,
  "type": "module"
}
module.exports = {
  client: {
    service: {
      name: 'foo',
      url: 'https://example.org/graphql',
    },
  },
}

Versions

v1.19.3

Additional Information

const defaultFileNames = [
"package.json",
`${MODULE_NAME}.config.js`,
`${MODULE_NAME}.config.ts`
];

I'm not sure but a possible solution could be also accepting apollo.config.cjs as a supported config file option. Should be tested

@alexanderniebuhr
Copy link
Contributor

@PS1TD
Copy link

PS1TD commented Jul 12, 2023

Having the same issue with the latest versions.
Using type: module
Trying to add apollo.config.js
Giving me error ERR_REQUIRE_ESM

If I switch to .cjs Apollo thinks I am missing the config file.

@abdalla-alHosainy
Copy link

Any updates on this issue?

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

Successfully merging a pull request may close this issue.

4 participants