JaneaSystems / nodejs-mobile

Full-fledged Node.js on Android and iOS
https://code.janeasystems.com/nodejs-mobile
Other
2.58k stars 182 forks source link

Cannot launch node.js due to undefined not being an object #157

Open J-Cake opened 5 years ago

J-Cake commented 5 years ago

Hi, I'm trying to run a basic node backend so I can use a node module that isn't supported by react-native, I try this project and I get an error, undefined is not an object. I work out that the issue comes from the call to node.start('server.js'). commenting it out, removes the error, but firstly, why does this happen and secondly how can I resolve it?

Errors below:

componentWillMount() {
    nodejs.start('server.js');
    nodejs.channel.addListener('message', msg => {alert('From node: ' + msg)}, this);
}

Android 9.0 Emulator Windows 10 1803 Home React-native version 0.57.8 Node.JS version 10.7 Java 10.0.2 Gradle 4.6

jaimecbernardo commented 5 years ago

Hi @KlaussMC , It might be because this line is not added to your react-native application:

import nodejs from 'nodejs-mobile-react-native';

The actual full log message from react-native or repo with reproduction steps might help in understanding the problem.

J-Cake commented 5 years ago

Hi @jaimecbernardo ! That was the first thing I checked, I made sure to follow the documentation exactly, and made sure to follow all notes and comments as well. I have become very frustrated with it, so I may miss something obvious, but I have confirmed as much as possible with the documentation.

PS: thanks for adding the tags. I still haven't figured out how to do that 😁

jaimecbernardo commented 5 years ago

Hi @KlaussMC , Can you please share the whole error log, then? 😄 Or the shared repo.

There isn''t enough information to see what's going on, this way.

J-Cake commented 5 years ago

Hi @jaimecbernardo Sorry, of course you need more information, I should've known that 😬 .

Anyway, here's the stacktrace on the phone (android) And there's nothing unusual in the gradle console / server

undefined is not a function (evaluating 'reactDevTools.connectToDevTools')
<unknown>
    C:\Users\*******\Code\Other\YTDownloader\node_modules\react-native\Libraries\Core\Devtools\setupDevtools.js:50:36
loadModuleImplementation
    C:\Users\*******\Code\Other\YTDownloader\node_modules\metro\src\lib\polyfills\require.js:320:6
guardedLoadModule
    C:\Users\*******\Code\Other\YTDownloader\node_modules\metro\src\lib\polyfills\require.js:214:36
metroRequire
    C:\Users\*******\Code\Other\YTDownloader\node_modules\metro\src\lib\polyfills\require.js:136:24
<unknown>
    C:\Users\*******\Code\Other\YTDownloader\node_modules\react-native\Libraries\Core\InitializeCore.js:211:13
loadModuleImplementation
    C:\Users\*******\Code\Other\YTDownloader\node_modules\metro\src\lib\polyfills\require.js:320:6
guardedLoadModule
    C:\Users\*******\Code\Other\YTDownloader\node_modules\metro\src\lib\polyfills\require.js:207:45
metroRequire
    C:\Users\*******\Code\Other\YTDownloader\node_modules\metro\src\lib\polyfills\require.js:136:24
global code
    C:\Users\*******\Code\Other\YTDownloader\App.js:47:4
jaimecbernardo commented 5 years ago

Not sure what it may be. I've tried replicating the issue on that react-native version, without success.

I've run these commands:

react-native init --version 0.57.8 TestProject
cd TestProject
npm install nodejs-mobile-react-native
react-native link nodejs-mobile-react-native
rename nodejs-assets\nodejs-project\sample-main.js main.js
rename nodejs-assets\nodejs-project\sample-package.json package.json

Add lines to App.js according to https://code.janeasystems.com/nodejs-mobile/getting-started-react-native :

import {Button} from 'react-native';

import nodejs from 'nodejs-mobile-react-native';

...

  componentWillMount()
  {
    nodejs.start('main.js');
    nodejs.channel.addListener(
      'message',
      (msg) => {
        alert('From node: ' + msg);
      },
      this 
    );
  }

...

    <Button title="Message Node"
    onPress={() => nodejs.channel.send('A message!')}
    />

With an Android 9.0 emulator running, I've run:

react-native run-android

It seems to work as expected. Are your replication steps any different?