Skip to content

Commit

Permalink
refactor: add named export in next/server (#39381)
Browse files Browse the repository at this point in the history
## Bug
When using next/server in different environment ([vitest](https://vitest.dev/)), named import will have runtime error.

```tsx
import { NextRequest } from 'next/server'
// TypeError: NextRequest is not a constructor
const request = new NextRequest()
```
This is conflicted with current `server.d.ts` files.

## Alternative
* reverte current `server.js` to esm, them compiled it to cjs.
  • Loading branch information
promer94 committed Aug 7, 2022
1 parent b1de1d1 commit dc13c12
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/next/server.js
Expand Up @@ -14,4 +14,13 @@ if (typeof URLPattern !== 'undefined') {
serverExports.URLPattern = URLPattern
}

// https://nodejs.org/api/esm.html#commonjs-namespaces
// When importing CommonJS modules, the module.exports object is provided as the default export
module.exports = serverExports

// make import { xxx } from 'next/server' work
exports.NextRequest = serverExports.NextRequest
exports.NextResponse = serverExports.NextResponse
exports.userAgentFromString = serverExports.userAgentFromString
exports.userAgent = serverExports.userAgent
exports.URLPattern = serverExports.URLPattern

0 comments on commit dc13c12

Please sign in to comment.