uhop / node-re2

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

Node unable to import re2 #190

Closed adityak2502 closed 9 months ago

adityak2502 commented 10 months ago

I am using node. When I do, var RE2 = require("re2");. I get

Uncaught runtime errors:
×
ERROR
Cannot set properties of undefined (setting 'Symbol(Symbol.match)')
TypeError: Cannot set properties of undefined (setting 'Symbol(Symbol.match)')
    at ./node_modules/re2/re2.js (http://localhost:3000/static/js/bundle.js:91123:48)
    at options.factory (http://localhost:3000/static/js/bundle.js:93171:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:92606:33)
    at fn (http://localhost:3000/static/js/bundle.js:92828:21)
    at ./src/index.js (http://localhost:3000/static/js/bundle.js:3332:11)
    at options.factory (http://localhost:3000/static/js/bundle.js:93171:31)
    at __webpack_require__ (http://localhost:3000/static/js/bundle.js:92606:33)
    at http://localhost:3000/static/js/bundle.js:93817:37
    at http://localhost:3000/static/js/bundle.js:93819:12

In index.js: var RE2 = require("re2");

./node_modules/re2/re2.js :

if (typeof Symbol != 'undefined') {
  Symbol.match &&
    (RE2.prototype[Symbol.match] = function (str) {
      return this.match(str);
    });

Symbol.match is on line 6

uhop commented 10 months ago

Do you add re2 as a dependency to your package.json? If you do, do you run npm i?

You can do both in one go: npm I --save re2.

Then you can try to export it.

adityak2502 commented 10 months ago

Yes. To reproduce:

npx create-react-app tmp
cd tmp
npm install --save re2

In src/index.js Add the following var RE2 = require("re2");

Then npm run start

This will open http://localhost:3000/ and you'll get the same error

uhop commented 10 months ago

You still create a React application. And it looks like you run it with a server.

Example (do it yourself):

✔ 23:10 ~/temp $ npm init -y
Wrote to ~/temp/package.json:

{
  "name": "temp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
✔ 23:10 ~/temp $ npm i --save re2

added 132 packages, and audited 133 packages in 2m

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

found 0 vulnerabilities
npm notice
npm notice New major version of npm available! 9.8.0 -> 10.1.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.1.0
npm notice Run npm install -g npm@10.1.0 to update!
npm notice
✔ 23:12 ~/temp $ node -e "console.dir(require('re2'))"
[Function: RE2] {
  getUtf8Length: [Function (anonymous)],
  getUtf16Length: [Function (anonymous)],
  unicodeWarningLevel: 'nothing'
}

In any case, I have no time to learn what create-react-app does and what kind of npm run start it creates. Do the commands above and investigate what is the difference between this simple example and whatever is created by the utility.

uhop commented 9 months ago

Closing for inactivity.

PS: Working with packagers like webpack I've noticed that they usually ignore imports with non-textual files, like re2.node. I suggest to file a bug with them.