question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API function error when Nextjs app has package type=module

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101
Binaries:
  Node: 16.15.0
  npm: 8.5.5
  Yarn: 1.22.15
  pnpm: 6.11.0
Relevant packages:
  next: 12.1.5
  react: 18.1.0
  react-dom: 18.1.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

branch trigger

Describe the Bug

Getting error when calling an api route when my package.json type=module

2022-05-14T00:47:32.805Z	undefined	ERROR	ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///var/task/___next_launcher.js:2:23
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async getListener (/var/task/___vc/__launcher.js:37:11)
RequestId: 53046e6c-3224-48d2-9428-1db2456069c2 Error: Runtime exited with error: exit status 1
Runtime.ExitError

Expected Behavior

Run function without error

To Reproduce

create api function with .ts in a nextjs app with it’s package.json type=module

deploy function to vercel

trigger production function

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ijjkcommented, May 19, 2022

The above PR is not yet available, we will update here when it is rolled

0reactions
github-actions[bot]commented, Jun 20, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with backend-side ES6 imports with "type":"module" with ...
Describe the Bug. I have an Express.js server that sets up Next.js, and I want to use ES6 modules with my backend. My...
Read more >
ERR_REQUIRE_ESM and Package.json error on Next.js. after ...
This error is fixed when removing "type": "module" from package.json . Support for using ESM-only node_modules is still fine.
Read more >
module-not-found - Next.js
A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies;...
Read more >
Solved: @arcgis/core with Next JS Error [ERR_REQUIRE_ESM]
Solved: My intention is to use ArcGis API in a Next JS application. I have used `create next app` to create a simple...
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
In this article, I'll walk you through an error you may get when you are building JavaScript apps with Next.js, and how to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found