nuxt: global.URL is missing
Version
Reproduction link
https://codesandbox.io/s/ww5n6p5mll
Steps to reproduce
Accessing URL object like new URL('http://example.com'); anywhere in server side code.
What is expected ?
URL object is accessible globally in server side since it is defined as global interface in node.js. https://nodejs.org/docs/latest-v10.x/api/globals.html#globals_url
What is actually happening?
Accessing URL object in server side code causes “ReferenceError URL is not defined”.
Additional comments?
It had been accessible before and including v2.3.4.
<div align="right">This bug report is available on Nuxt community (#c8582)</div>About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 9
- Comments: 20 (4 by maintainers)
This is because we’re using
runInNewContext: trueby default in dev mode for avoiding memory leak, but vue does’t injectURLin sandbox global https://github.com/vuejs/vue/blob/master/src/server/bundle-renderer/create-bundle-runner.js#L9.You can use
runInNewContext: false, but it may cause unexpected memory leak in dev mode.So I created a pr in Vue for adding URL https://github.com/vuejs/vue/pull/10414.
Another tricky way is using require(‘url’) which will require module outside new context:
And,
npm run devreproduces, whilenpm run build && npm startdoes not.Here’s a temporary workaround that pulls from the appropriate place depending on the context.
And you just pull it in like this
import { URL, URLSearchParams } from './url'Hi @pi0 this should be fixed in the next release according to your recent merge?
Another (slightly altered) temporary workaround:
In my case, I’m importing a module that uses URL from the vue file. There are two or three ways to reproduce my case.
URLdom object"lib": ["dom"]npm i @types/nodeand/or
VSCode throws error that
Urlis a type/interface but it’s used as a value in first case. In both cases, they ‘work’ despite VSCode throwing error, but doesn’t work properly. For example:prints a Url instance where all properties are
null.Third or second case:
just throws reference error that URL is not defined.
As expected,
URLworks just fine in the vue files.Node version: 10.2.1 Nuxt version: 2.4.0
I looked into this a bit and it seems that eg URL is (and has been) a non-enumerable property on the
globalobject. Egglobal.Buffercan still be used but that is enumerable (for now, it will become non-enumerable in v12 probably).Not sure where to look further for this, but probably its a babel / core-js issue? As a side note, core-js v3 has a URL polyfill.