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

feat: esm format #12

Closed
privatenumber opened this issue Mar 4, 2021 · 3 comments
Closed

feat: esm format #12

privatenumber opened this issue Mar 4, 2021 · 3 comments
Labels

Comments

@privatenumber
Copy link
Contributor

privatenumber commented Mar 4, 2021

Currently, when importing native ESM packages ("type": "module"), I get the following error:

internal/modules/cjs/loader.js:1080
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/pvtnbr/Documents/public-github/privatenumber/vue-search/node_modules/fs.promises.exists/index.js
require() of ES modules is not supported.
require() of /Users/pvtnbr/Documents/public-github/privatenumber/vue-search/node_modules/fs.promises.exists/index.js from /Users/pvtnbr/Documents/public-github/privatenumber/vue-search/src/index.ts 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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/pvtnbr/Documents/public-github/privatenumber/vue-search/node_modules/fs.promises.exists/package.json.

    at Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Object.newLoader [as .js] (/Users/pvtnbr/Documents/public-github/privatenumber/vue-search/node_modules/.pnpm/pirates@4.0.1/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/Users/pvtnbr/Documents/public-github/privatenumber/vue-search/src/index.ts:4:22)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Module._compile (/Users/pvtnbr/Documents/public-github/privatenumber/vue-search/node_modules/.pnpm/pirates@4.0.1/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10) {
  code: 'ERR_REQUIRE_ESM'
}

I believe this is happening because esbuild is converting the esm import to cjs requires.

Seems like there's an overrides option to pass in format: 'esm' here, but I'm guessing this is only available for the Node.js API.

Can esbuild-register pass in format: 'esm' to esbuild if a flag is set or if the current project package.json has type: 'module' set?

@egoist egoist closed this as completed in fcfd99f Mar 4, 2021
@egoist
Copy link
Owner

egoist commented Mar 4, 2021

🎉 This issue has been resolved in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@egoist egoist added the released label Mar 4, 2021
@privatenumber
Copy link
Contributor Author

Amazing, thank you @egoist

@eric-burel
Copy link

eric-burel commented Mar 11, 2022

Any idea about this one?

Converting "require" to "esm" is currently not supported
{
  column: 30,
  file: '/code/vulcan-npm/starters/express/.yalc/@vulcanjs/mongo/dist/index.js',
  length: 7,
  line: 38,
  lineText: 'var import_mongoose = __toESM(require("mongoose"));',
  namespace: '',
  suggestion: ''
}
Converting "require" to "esm" is currently not supported

I got a few errors like this (can provide repro if needed). I seem to hit evanw/esbuild#566

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

3 participants