unjs / unenv

🕊️ Convert javaScript code to be runtime agnostic
MIT License
444 stars 22 forks source link

[wrangler] "default" is not exported by "node_modules/unenv/runtime/polyfill/process.mjs" #260

Open jvbianchi opened 4 months ago

jvbianchi commented 4 months ago

Environment

cloudflare pages

Reproduction

try to deploy Nuxt to cloudflare pages using the /server/api/hello.ts folder with a simple test

export default defineEventHandler((_event) => {
  return {
    hello: "Hello from Server!!!",
  };
});

Describe the bug

"default" is not exported by "node_modules/unenv/runtime/polyfill/process.mjs"

Additional context

unenv-nightly version 1.10.0-1717606461.a117952

Logs

19:41:10.273    [info] [nitro] Building Nuxt Nitro server (preset: `cloudflare-pages`)
19:41:11.891    node_modules/std-env/dist/index.mjs (1:9): Error when using sourcemap for reporting an error: Can't resolve original location of error.
19:41:11.891    [error] [nitro] RollupError: node_modules/std-env/dist/index.mjs (1:9): "default" is not exported by "node_modules/unenv/runtime/polyfill/process.mjs", imported by "node_modules/std-env/dist/index.mjs".
19:41:11.892    
19:41:11.892    
19:41:11.892    1: import { default as process } from 'unenv/runtime/polyfill/process';
19:41:11.892                ^
19:41:11.892    2: 
19:41:11.892    3: const r=Object.create(null),E=e=>globalThis.process?.env||globalThis._importMeta_.env||globalThis.Deno?.env.toObject(...
19:41:11.893    [error] node_modules/std-env/dist/index.mjs (1:9): "default" is not exported by "node_modules/unenv/runtime/polyfill/process.mjs", imported by "node_modules/std-env/dist/index.mjs".
19:41:11.894      at getRollupError (node_modules/rollup/dist/es/shared/parseAst.js:396:41)
19:41:11.894      at error (node_modules/rollup/dist/es/shared/parseAst.js:392:42)
19:41:11.894      at Module.error (node_modules/rollup/dist/es/shared/node-entry.js:13858:16)
19:41:11.894      at Module.traceVariable (node_modules/rollup/dist/es/shared/node-entry.js:14306:29)
19:41:11.894      at ModuleScope.findVariable (node_modules/rollup/dist/es/shared/node-entry.js:11984:39)
19:41:11.894      at Identifier.bind (node_modules/rollup/dist/es/shared/node-entry.js:6908:40)
19:41:11.894      at UnaryExpression.bind (node_modules/rollup/dist/es/shared/node-entry.js:4775:23)
19:41:11.894      at BinaryExpression.bind (node_modules/rollup/dist/es/shared/node-entry.js:4775:23)
19:41:11.895      at LogicalExpression.bind (node_modules/rollup/dist/es/shared/node-entry.js:4775:23)
19:41:11.895      at LogicalExpression.bind (node_modules/rollup/dist/es/shared/node-entry.js:4775:23) 
19:41:11.895    
19:41:11.895    [error] node_modules/std-env/dist/index.mjs (1:9): "default" is not exported by "node_modules/unenv/runtime/polyfill/process.mjs", imported by "node_modules/std-env/dist/index.mjs".
19:41:11.930    Failed: Error while executing user command. Exited with error code: 1
pi0 commented 4 months ago

Are you force resolving unenv to nighyly version in your Nitro project?

jvbianchi commented 4 months ago

No , I just install nuxt using Bun with this command:

bun create cloudflare@latest my-nuxt-app -- --framework=nuxt
pi0 commented 4 months ago

You have mentioned unenv-nightly version 1.10.0-1717606461.a117952 how it is there?

Can you please share npm why unenv / pnpm why unenv (depending on your package manager)

jvbianchi commented 4 months ago

I just saw, It's one of wrangler's depencies:

wrangler@3.60.3:
  version "3.60.3"
  resolved "https://registry.npmjs.org/wrangler/-/wrangler-3.60.3.tgz"
  integrity sha512-a6zn/KFnYaYp3nxJR/aP0TeaBvJDkrrfI89KoxUtx28H7zpya/5/VLu3CxQ3PRspEojJGF0s6f3/pddRy3F+BQ==
  dependencies:
    unenv "npm:unenv-nightly@1.10.0-1717606461.a117952"
    nanoid "^3.3.3"
    esbuild "0.17.19"
    resolve "^1.22.8"
    chokidar "^3.5.3"
    miniflare "3.20240610.0"
    selfsigned "^2.0.1"
    source-map "0.6.1"
    blake3-wasm "^2.1.5"
    xxhash-wasm "^1.0.1"
    path-to-regexp "^6.2.0"
    resolve.exports "^2.0.2"
    "@cloudflare/kv-asset-handler" "0.3.2"
    "@esbuild-plugins/node-globals-polyfill" "^0.2.3"
    "@esbuild-plugins/node-modules-polyfill" "^0.2.2"
  optionalDependencies:
    fsevents "~2.3.2"

It was changed in this commit: https://github.com/cloudflare/workers-sdk/commit/1e68fe5448ffa4d0551dc7255405983c329235c8

IgorMinar commented 4 months ago

Interesting. Is this some kind of weird npm package hoisting issue?

I've just tested the repro with pnpm and it works just fine for me, and I see

$ pnpm list --depth=20 unenv unenv-nightly
Legend: production dependency, optional only, dev only

nuxt-app /my-nuxt-app2

dependencies:
nuxt 3.12.2
├─┬ @nuxt/vite-builder 3.12.2
│ ├─┬ h3 1.11.1
│ │ └── unenv 1.9.0
│ └── unenv 1.9.0
├─┬ h3 1.11.1
│ └── unenv 1.9.0
├─┬ nitropack 2.9.6
│ ├─┬ h3 1.11.1
│ │ └── unenv 1.9.0
│ ├─┬ listhen 1.7.2
│ │ └─┬ h3 1.11.1
│ │   └── unenv 1.9.0
│ ├── unenv 1.9.0
│ └─┬ unstorage 1.10.2
│   ├─┬ h3 1.11.1
│   │ └── unenv 1.9.0
│   └─┬ listhen 1.7.2
│     └─┬ h3 1.11.1
│       └── unenv 1.9.0
├── unenv 1.9.0
└─┬ unstorage 1.10.2
  └─┬ h3 1.11.1
    └── unenv 1.9.0

devDependencies:
h3 1.11.1
└── unenv 1.9.0
wrangler 3.60.3
└── unenv-nightly 1.10.0-1717606461.a117952
IgorMinar commented 4 months ago

Could you try to use pnpm or somehow tell bun not to hoist packages (I'm not a bun user, but I hope there is a way to do that).

@pi0 we could try to bundle unenv-nightly into Wrangler but it's tricky because unenv has built-time and runtime component and we currently get the runtime-component from the npm package (the built-time part is already bundled into the wrangler bundle).

jvbianchi commented 4 months ago

When I install unenv as a dev dependency (version 1.9.0) the error goes way.

IgorMinar commented 4 months ago

@jvbianchi which suggests that bun's package hoisting is the culprit here.

pi0 commented 4 months ago

Thanks for report dear @jvbianchi

As you have already figured out, adding latest (1.9.x) version of unenv to your main project dependencies, devDependencies (or resolutions if those won't work will fix the hoisting issue in your project.

For the followup:

DavidFrings commented 1 week ago

You have mentioned unenv-nightly version 1.10.0-1717606461.a117952 how it is there?

Can you please share npm why unenv / pnpm why unenv (depending on your package manager)

I have the same Issue with @nuxtjs/sitemap...

pnpm why unev:

dependencies:
nuxt 3.13.2
├─┬ @nuxt/vite-builder 3.13.2
│ ├─┬ h3 1.13.0
│ │ └── unenv 1.10.0
│ └── unenv 1.10.0
├─┬ h3 1.13.0
│ └── unenv 1.10.0
├─┬ impound 0.1.0
│ └── unenv 1.10.0
├─┬ nitropack 2.9.7
│ ├─┬ h3 1.13.0
│ │ └── unenv 1.10.0
│ ├─┬ listhen 1.9.0
│ │ └─┬ h3 1.13.0
│ │   └── unenv 1.10.0
│ ├── unenv 1.10.0
│ └─┬ unstorage 1.12.0
│   ├─┬ h3 1.13.0
│   │ └── unenv 1.10.0
│   └─┬ listhen 1.9.0
│     └─┬ h3 1.13.0
│       └── unenv 1.10.0
└── unenv 1.10.0

devDependencies:
@nuxtjs/sitemap 6.1.2
└─┬ h3-compression 0.3.2
  └─┬ h3 1.13.0 peer
    └── unenv 1.10.0