egoist / esbuild-register

Transpile JSX, TypeScript and esnext features on the fly with esbuild
MIT License
984 stars 52 forks source link

feat: esm format #12

Closed privatenumber closed 3 years ago

privatenumber commented 3 years ago

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 commented 3 years ago

:tada: This issue has been resolved in version 2.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

privatenumber commented 3 years ago

Amazing, thank you @egoist

eric-burel commented 2 years ago

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 https://github.com/evanw/esbuild/issues/566