Skip to content

Import tfjs-react-native not working on EXPO CLI Project #2784

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

Closed
DanielOnye123 opened this issue Feb 23, 2020 · 19 comments
Closed

Import tfjs-react-native not working on EXPO CLI Project #2784

DanielOnye123 opened this issue Feb 23, 2020 · 19 comments
Assignees
Labels
comp:react-native type:bug Something isn't working type:support user support questions

Comments

@DanielOnye123
Copy link

Describe the problem or feature request

Screen Shot 2020-02-23 at 12 04 47 AM

I'm getting this error when I try to run my React-Native program on the web or android. I followed the steps on the tfjs-react-native documentation but I'm still having issues with my program.

TensorFlow.js version

tfjs-react-native

Browser version

package.json
Screen Shot 2020-02-23 at 12 15 50 AM

Code to reproduce the bug / link to feature request

This is the code causing the error
Screen Shot 2020-02-23 at 12 17 50 AM

I even removed the tfjs-react native import and it compiles so I don't know what to do

If you would like to get help from the community, we encourage using Stack Overflow and the tensorflow.js tag.

GitHub issues for this repository are tracked in the tfjs union repository.

Please file your issue there, following the guidance in that issue template.

@tafsiri
Copy link
Contributor

tafsiri commented Feb 25, 2020

From your error it looks like you are using webpack (as opposed to metro) to bundle your app. I think you need to configure webpack to process .js files (or even that specific js file) with the JSX loader (or some transformer that can consume jsx syntax).

@tafsiri tafsiri added comp:react-native type:support user support questions labels Feb 25, 2020
@tafsiri tafsiri self-assigned this Feb 25, 2020
@DanielOnye123
Copy link
Author

Screen Shot 2020-02-25 at 6 12 28 PM

I noticed I'm using babel.config.js rather than webpack.config.js. I've tried to look upon adding a babel-loader in this config file and I'm honestly lost on the process. Would you happen to know how to do so?

@muditgoel01
Copy link

muditgoel01 commented Feb 26, 2020

It doesn't seem related to JSX syntax because other JSX components are being rendered properly. The problem occurs as soon as I import @tensorflow/tfjs-react-native.

From the Error Log, could it be related to this: microsoft/TypeScript#30650

I am using Expo Managed app.

@tafsiri
Copy link
Contributor

tafsiri commented Feb 26, 2020

@DanielOnye123 I don't know how to do this. I haven't been using webpack in concert with RN apps.

@muditgoel01 I don't think its related to typescript as the files in the package are already transpiled to JavaScript. In this case it may be that the bundler is configured to assume all components are in files with the .jsx extension. Are you able to load JSX components that are within .js files?

@tafsiri
Copy link
Contributor

tafsiri commented Feb 26, 2020

I would suggest asking on Stack Overflow or the Expo forums for more info on configuration options for the setup you are using. It could be a recent change in expo.

@tafsiri
Copy link
Contributor

tafsiri commented Feb 26, 2020

Could you test this by
1. removing the import of the library
2. Make a small react component in a file with a .js extension
3. import and render that component

If that works, then it helps narrow down where a configuration issue may be.

@DanielOnye123
Copy link
Author

I'm able to import and render without the tfjs-react module. I do believe this could be a configuration issue because I tested this on an Expo managed app with typescript configuration and the import worked

@muditgoel01
Copy link

Downgrading to "@tensorflow/tfjs-react-native": "0.1.0-alpha.2" works fine.
"@tensorflow/tfjs-react-native": "0.1.0-alpha.3" also gives same error.

@tafsiri
Copy link
Contributor

tafsiri commented Feb 26, 2020

@DanielOnye123 are you able to import a JSX component that is in a .js file?

@DanielOnye123
Copy link
Author

@tafsiri Yes. I am able to

@tafsiri tafsiri added the type:bug Something isn't working label Feb 27, 2020
@pvaneck
Copy link
Contributor

pvaneck commented Feb 27, 2020

@DanielOnye123 Were you getting this unexpected token error in your project only when doing yarn web (expo start --web)? Webpack is used for running react-native apps in the browser with expo. You shouldn't be hitting this specific error when running on Android.

Try running expo customize:web in the root of the project, then select the webpack.config.js option to generate the webpack config file. Add the following to the webpack.config.js file and try re-running the app.

const createExpoWebpackConfigAsync = require('@expo/webpack-config');
const path = require('path');

module.exports = async function(env, argv) {
  const config = await createExpoWebpackConfigAsync(env, argv);
  
  config.module.rules.push({
    test: /\.(js|jsx)$/,
    include: [path.resolve(__dirname, "node_modules/@tensorflow/tfjs-react-native")],
    use: {
      loader: 'babel-loader',
      options: {
        presets: [
          ['@babel/env', { modules: 'commonjs' }], 
        ],
      }
    }
  });
  return config;
};

I was able to get my managed Expo JS app working on the web with this.

@DanielOnye123
Copy link
Author

Screen Shot 2020-02-28 at 12 29 09 AM

Thank you. It's working on the web, but ironically it's not working on the android. I keep getting this error above. I checked out this other documentation with a similar issue: Issue, but his solution didn't work for me as I don't have typescript config

@pvaneck
Copy link
Contributor

pvaneck commented Feb 28, 2020

That compiled_api.ts file will be removed from the dist directory in the next tfjs release (with fb163f7) as it shouldn't even be there. For a quick fix, just delete that typescript file:

rm -f node_modules/@tensorflow/tfjs-converter/dist/src/data/compiled_api.ts

Then metro should stop complaining.

@DanielOnye123
Copy link
Author

Thank you so much! It's working on both platforms. My last question is: Does the "fetch" method imported from tfjs-react native support local fetches? e.g locally fetching a picture

@motya770
Copy link

motya770 commented Mar 13, 2020

Hi, I don't have experience with ReactNative/NodeJs and just tried everything you explained here, but still, have the same problems. Can you please, provide the example code?. I am running on MacOs, node 13, expo cli. - My hello world RN app is working, but when I add your code - I got the same errors.

@pvaneck
Copy link
Contributor

pvaneck commented Mar 13, 2020

Doesn't seem like fetch supports local afaik. Might have to use expo-file-system to load local images for now.

@motya770 Which error are you getting? I have a barebones TF.js typescript expo managed project that can load fine for me (https://github.com/pvaneck/tfjs-react-native-app) if you want to check that out.

@AbdulRashidReshamwala
Copy link

@pvaneck fetch seems to be working fine in ios but fails on android

@motya770
Copy link

motya770 commented May 1, 2020

Hi, I just switched to Flutter, much simpler, much more predictable results )

@tafsiri
Copy link
Contributor

tafsiri commented May 19, 2020

Looks like this has been resolved. +1 to the comment on using something like expo-file-system to read local files. Feel free to leave a comment/file a new issue if this is still unresolved.

@tafsiri tafsiri closed this as completed May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:react-native type:bug Something isn't working type:support user support questions
Projects
None yet
Development

No branches or pull requests

6 participants