microlinkhq / metascraper

Get unified metadata from websites using Open Graph, Microdata, RDFa, Twitter Cards, JSON-LD, HTML, and more.
https://metascraper.js.org
MIT License
2.35k stars 168 forks source link

Error `Cannot find module './build/Release/re2'` with Next.js 12 deployed to Vercel #468

Closed jca41 closed 3 years ago

jca41 commented 3 years ago

Prerequisites

Subject of the issue

After upgrading to Nextjs.12 (12.0.2) I'm now getting the error below when deploying to Vercel. I'm using metascraper in a API route (node). I've tried both yarn and npm by pushing a lock file, but the issue still persists.

I've seen the error locally intermittently but it fixes itself after removing node_modules and npm i. When deployed to Vercel it always fails. Vercel is using node 14.x apparently.

ERROR   Error: Cannot find module './build/Release/re2'
Require stack:
- /var/task/node_modules/re2/re2.js
- /var/task/node_modules/url-regex-safe/lib/index.js
- /var/task/node_modules/@metascraper/helpers/index.js
- /var/task/node_modules/metascraper/src/index.js
- /var/task/.next/server/pages/api/metadata.js
- /var/task/node_modules/next/dist/server/next-server.js
- /var/task/___next_launcher.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/var/task/node_modules/re2/re2.js:3:13)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/node_modules/re2/re2.js',
    '/var/task/node_modules/url-regex-safe/lib/index.js',
    '/var/task/node_modules/@metascraper/helpers/index.js',
    '/var/task/node_modules/metascraper/src/index.js',
    '/var/task/.next/server/pages/api/metadata.js',
    '/var/task/node_modules/next/dist/server/next-server.js',
    '/var/task/___next_launcher.js',
    '/var/runtime/UserFunction.js',
    '/var/runtime/index.js'
  ]
}

Screenshot 2021-11-01 at 15 22 53

Steps to reproduce

Note: You can reproduce the code using interactive Node.js shell by Runkit.

Deploy to an API route to Vercel with Next.js 12.

Expected behaviour

Should require re2.

Actual behaviour

We get a Error: Cannot find module './build/Release/re2' error.

Kikobeats commented 3 years ago

Hello, there is not too much I can't help there – the binary is missing, so you need to figure out why.

what happens under Next 12.0.1?

jca41 commented 3 years ago

Hello, there is not too much I can't help there – the binary is missing, so you need to figure out why.

what happens under Next 12.0.1?

It works fine with Next.js 11

Kikobeats commented 3 years ago

Hmm probably It's a thing related to webpack since Next.js 12:

After making webpack 5 the default in Next.js 11, we've now officially removed webpack 4. We've worked closely with the community to ensure a smooth transition to webpack 5.

jca41 commented 3 years ago

Thanks, I will get a minimal reproduction and ask the Vercel guys