Search code examples
react-nativesystemjs

SystemJS with React native


I want to use systemjs inside react-native.

When I use following code in the browser systemjs register itself with global/window.

fetch('https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.18.17/system.src.js')
      .then(response => response.text())
      .then(txt => {
        eval(txt);
      })
      .catch(ex => {
        console.log(ex);
      });

I executed same code in expo AwesomeProject on the homescreen component and the above code does not throw any exceptions, but when I try to render all the globally registered objects I don't see SystemJs/System.

  1. First question I have - Is it even possible to register systemjs and use it with react-native?

  2. If the answer to first question is yes, then is it the correct way to eval(Systemjs)?

  3. If not is there any other method I can use to access system js?

I have seen couple of projects on github like react-native-eval and react auto updater, but that is not what I want.

Anyone can give me some insight?


Solution

  • I will answer my own question. Yes it is possible to load systemjs in React native.

    Simple eval will evaluate the systemjs code, but you might get some errors. I am not an expert in the internals of systemjs but it seems it tries to run in some context, either a browser or in some cases a node server.

    I received an error in following code.

    var vmModule = 'vm'; var vm = require(vmModule);

    I had to tweak my code a little bit and supply a fake require function and I was able to load systemjs.