Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'slice' of null (Create React App project) #154

Open
lnhrdt opened this issue Jun 11, 2018 · 7 comments
Open

Cannot read property 'slice' of null (Create React App project) #154

lnhrdt opened this issue Jun 11, 2018 · 7 comments

Comments

@lnhrdt
Copy link

lnhrdt commented Jun 11, 2018

instascan throws an error in my projects:

TypeError: Cannot read property 'slice' of null
parseJSFunc
node_modules/instascan/src/zxing.js:4

The projects are created with Create React App 2. CRA2 is currently in alpha but is otherwise very stable and instascan is the first dependency I've come across that does not work. Perhaps the issue is within instascan.

I originally reported an issue in react-instascan (rubenspgcavalcante/react-instascan/issues/9) but the author looked into it and believes the issue is within instascan.

I've created a repository that reproduces the error:
https://github.com/lnhrdt/react-instascan-error

Any ideas?

@rubenspgcavalcante
Copy link

Hi, just to give more info about the problem. In the create-react-app example from @lnhrdt , if you just overwrite the entire index.js with...

import {Camera} from "instascan";
Camera.getCameras().then(cameras => console.log(cameras));

...the error will happen.
What I saw is some problem with the internal resolution of that script in the code. I noted this zxing script (minified) is added directly to the project and not as a dependence (should this bundled file came from this project?)
Anyway if this is the case, maybe just add that package, using npm/yarn instead and check if the umd resolution works? 🤔

@lnhrdt
Copy link
Author

lnhrdt commented Jun 11, 2018

Thanks for the extra info @rubenspgcavalcante!

I just updated the example repo to remove the react-instascan dependency and demonstrate the error using just instascan, the way Rubens suggested.

https://github.com/lnhrdt/react-instascan-error

@rubenspgcavalcante
Copy link

rubenspgcavalcante commented Jun 12, 2018

@lnhrdt , maybe this one is also related #121
The error is different, but I still thinking that's something with the internal module resolution 🤔

@lnhrdt
Copy link
Author

lnhrdt commented Jun 13, 2018

Good insight @rubenspgcavalcante, seems likely. The instascan author hasn't participated in that discussion yet. @schmich do you have any insight in to this issue (which may be related to #121)? Your thoughts would be helpful here!

@karenjwap
Copy link

Error comes from a syntax change during transpilation.

in zxing.js, replace:
var sourceRegex=/^function\s*(([^)]))\s{\s*([^*]?)[\s;](?:return\s*(.?)[;\s])?}$/;

by:
var sourceRegex=/^function\s*\S*(([^)]))\s{\s*([^*]?)[\s;](?:return\s*(.?)[;\s])?}$/;

worked for me.

@augustofontes
Copy link

+1

@ZeeCoder
Copy link

That didn't work for me @karenjwap , but pointed me in the right direction.
Ideally the Emscripten JavaScript build of the C++ port of the ZXing Java library should be rebuilt, as it seems the current one might not be compatible with newer browsers.

Essentially an object with function as values gets turned into strings, then parsed for their arguments, body and return value.
The regex that's responsible for this pattern matching however does not account for function names, only nameless functions pass.

tldr Anyway, here's the solution:

Replace the sourceRegex bit with the following regex:

/^function[^\(]*\(([^)]*)\)\s*{\s*([^*]*?)[\s;]*(?:return\s*(.*?)[;\s]*)?}$/

Or just use a fork:
https://github.com/CrowdReactive/instascan

JaroslavPribyl added a commit to JaroslavPribyl/instascan that referenced this issue Sep 17, 2019
- set sourceRegex to /^function[^\(]*\(([^)]*)\)\s*{\s*([^*]*?)[\s;]*(?:return\s*(.*?)[;\s]*)?}$/ (see schmich#154 for more details)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants