uhop / node-re2

node.js bindings for RE2: fast, safe alternative to backtracking regular expression engines.
Other
479 stars 53 forks source link

not working with next.js 13 #167

Closed goodbbai closed 8 months ago

goodbbai commented 1 year ago

When I install re2 with next.js and try to run the code, it saids

Module not found: Can't resolve './build/Release/re2' at that path, there exists re2.node If I run test scripts without next.js, it works perfectly.

The way I install it is here. $ npx create-next-app@latest --ts test-re $ npm i re2

my code is here.

  const RE2 = require('re2')

  const sample= 'abbcdefabh';

  const re1 = new RE2('ab*', 'g');
  re1.test(sample);

I tried it on my intel mac and silicon mac, but the result is the same.

uhop commented 1 year ago

IIRC Next.js is a framework to write client-side React-based web applications. re2 is a binary extension for Node written in C++. It will not work in a browser.

I am not sure why someone needs re2 on a client, but there are several offerings that use wasm. For example:

I didn't try them so it is not an endorsement. But they can work on a client.

goodbbai commented 1 year ago

I'm not sure but is that a reason that Next.js can't resolve re2.node? I have known that the main feature of Next.js 13 is SSR and I put the 'require' into the server side, however, it's not worked as I said.

the reason I try to use that module is started to get metadata like a title and a publisher of a certain link. Almost every module to do that like metascraper and browserless(with cheerio) requires url-regex-safe package, and it uses re2.

uhop commented 1 year ago

I started to use the full file name since https://github.com/uhop/node-re2/releases/tag/1.18.2

Please try it and let me know if it helps or not.

elie222 commented 11 months ago

IIRC Next.js is a framework to write client-side React-based web applications. re2 is a binary extension for Node written in C++. It will not work in a browser.

I am not sure why someone needs re2 on a client, but there are several offerings that use wasm. For example:

I didn't try them so it is not an endorsement. But they can work on a client.

Next.js is both client and server side. The backend is serverless Node.js.

elie222 commented 11 months ago

I started to use the full file name since 1.18.2 (release)

Please try it and let me know if it helps or not.

Doesn't seem to work on 1.20.1.

le0pard commented 11 months ago

you can try to use https://re2js.leopard.in.ua/ - it is pure JS ported lib, so will work in browser and node.js env, but performance is worse, than have node-re2 and browser will need load good amount of JS code

image

uhop commented 9 months ago

OK, I tried using your original example with minor modifications:

$ npx create-next-app@latest --ts test-re
✔ Would you like to use ESLint? … No / Yes
✔ Would you like to use Tailwind CSS? … No / Yes
✔ Would you like to use `src/` directory? … No / Yes
✔ Would you like to use App Router? (recommended) … No / Yes
✔ Would you like to customize the default import alias (@/*)? … No / Yes
Creating a new Next.js app in /Users/eugene/Open/test-re.

Using npm.

Initializing project with template: app-tw

Installing dependencies:
- react
- react-dom
- next

Installing devDependencies:
- typescript
- @types/node
- @types/react
- @types/react-dom
- autoprefixer
- postcss
- tailwindcss
- eslint
- eslint-config-next

added 333 packages, and audited 334 packages in 3s

116 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Initialized a git repository.

Success! Created test-re at /Users/eugene/Open/test-re

✔ $ cd test-re/
✔ test-re [main L|✔] $ npm i --save re2

added 101 packages, and audited 435 packages in 2m

130 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Then I created a file index.js:

const RE2 = require('re2')

const sample= 'abbcdefabh';

const re1 = new RE2('ab*', 'g');
console.log(re1.test(sample));

Then I executed it:

$ node index.js
true

As you can see it WFM. What did I do wrong? Can you repeat what I did?

uhop commented 8 months ago

Closing for inactivity. I hope it was resolved.