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

No import() polyfill available for context 'node' #9616

Closed
vad-systems opened this issue Apr 3, 2024 · 2 comments · May be fixed by #9617
Closed

No import() polyfill available for context 'node' #9616

vad-systems opened this issue Apr 3, 2024 · 2 comments · May be fixed by #9617

Comments

@vad-systems
Copy link

vad-systems commented Apr 3, 2024

🐛 bug report

Because supportData['dynamic-import'] doesn't have an entry for node, dynamic imports for node targets with esm output cause the import()-polyfill check to trigger.

🎛 Configuration (package.json, cli command)

As per the repro repo:

package.json:

{
  "name": "@vad-systems/parcel-bug-repro",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "targets": {
    "server": {
      "context": "node",
      "outputFormat": "esmodule",
      "distDir": "build/server",
      "source": "src/server/index.ts",
      "engines": {
        "node": ">= 21"
      }
    }
  },
  "devDependencies": {
    "parcel": "2.12.0"
  },
  "scripts": {
    "build": "npx parcel build"
  }
}

cli command:

npm run build
> npx parcel build
>> parcel build

🤔 Expected Behavior

parcel bundles the target with node context correctly (forced for demonstration by injecting node: "1"):

$ npm run build
> @vad-systems/[email protected] build
> npx parcel build

✨ Built in 292ms

build/server/index.js           149 B    46ms
build/server/foo.1d7c16ab.js    233 B    53ms

with generated build/server/index.js:

var a={};a=import("./foo.1d7c16ab.js").then(()=>parcelRequire("7e5iT"));const{default:e}=await a;
//# sourceMappingURL=index.js.map

😯 Current Behavior

parcel errors out with the import()-polyfill check:

$ npm run build
> @vad-systems/[email protected] build
> npx parcel build

🚨 Build failed.

@parcel/runtime-js: No import() polyfill available for context 'node'

  Error: No import() polyfill available for context 'node'

💁 Possible Solution

Add node entry to supportData['dynamic-import'] as appropriate. I would argue this is 15.3 (the esm module was stabilized for 15.3.0, with backport to 14.17.0 and 12.22.0 - while it would be more accurate, I don't think it's necessary to include the backport versions at this point), and am about to provide a pull request to that effect.

🔦 Context

pointless prose for background

Granted, this is a somewhat odd setup, but I have quite a bit of shared code between the server and client bundles, and while it naturally doesn't make much sense to split the code on the server, code-splitting on the client is rather useful. If I make use of parcel-specific features (such as transformers for non-js files), `isLibrary: true` for the server target stops being a useful option without making the toolchain more complicated again. Similarly, I do need to compile the server for a `node` context because the non-shared code in that target relies on node dependencies. I could probably work around this by moving the shared code out to a library, but that also makes code-splitting more tedious.

I haven't populated the repro repo with these additional constraints, as the bug itself seems to just be the missing supportData entry. This is further supported by the fact that matchesEngine does have a branch for node which just returns false for lack of an entry, and the reference to caniuse-api can't be the source for the existing node entries for arrow-functions and global-this.

💻 Code Sample

minimal repro repo

🌍 Your Environment

Software Version(s)
Parcel 2.12.0
Node 18.16.0
npm 9.5.1
Operating System MacOS 14.4.1
Copy link

github-actions bot commented Oct 1, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

@TechQuery
Copy link

TechQuery commented Nov 21, 2024

@devongovett , do you have any suggestion about @vad-systems 's issue & PR?

I resolve it for now with this solution: https://remarkablemark.org/blog/2022/03/07/parcel-build-failed-no-import-polyfill-available/

And it may be related with #7877

@github-actions github-actions bot removed the Stale Inactive issues label Nov 21, 2024
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.

2 participants