1

I am trying to use Tau-Prolog with Node.js and React.

At the step:

session.consult(program);   

I get this error message:

TypeError: fs.existsSync is not a function

Here is the code to reproduce on the problem:

index.js

import React from 'react';
import ReactDOM from 'react-dom';

// These alternatives makes no difference:
var pl = require('tau-prolog');
// var pl = require('./tau-prolog/modules/core.js');
// var pl = require('./tau-prolog.js');

class App extends React.Component {
    componentDidMount() {
        let program = 'fruit(apple). fruit(banana).';
        let session = pl.create();
        // Until here, it's ok. I get Session {rules: {…}, src_predicates: {…},

        // The trouble is at this step:
        session.consult(program);
        ////////
        //////// TypeError: fs.existsSync is not a function
        ////////
    }
    
    render() {
        return <div>Hello world</div>;
    }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Thanks for your help!

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
yves
  • 11
  • 2
  • Do more research before asking a question: https://stackoverflow.com/questions/53841364/how-to-resolve-fs-existssync-is-not-a-function – Abdollah Mar 29 '20 at 08:24
  • Does this answer your question? [How to resolve fs.existsSync is not a function](https://stackoverflow.com/questions/53841364/how-to-resolve-fs-existssync-is-not-a-function) – Abdollah Mar 29 '20 at 08:25
  • JavaScript is one of the worst things of IT. – David Tonhofer Mar 29 '20 at 08:26
  • @Abdollah: Your edits are no improvement whatsoever. – false Mar 29 '20 at 09:44
  • @DavidTonhofer: I find Javascript one of the best... when I look at it as the 'battle proven survivor' to Microsoft world domination... – CapelliC Mar 29 '20 at 16:10
  • This is a bug in tau-prolog in the way it detects the node/browser environment. Looks like it should be fixed in github shortly. (9 April 2020). – meesern Apr 09 '20 at 06:53

1 Answers1

2

You don't require it on server side, except if you want to evaluate the prolog program on server side and request the result.

If you just want to use it with react in the frontend, you can simply load it like any other frontend library by adding it to your html

<script type="text/javascript" src="tau-prolog.js"></script>
<script type="text/javascript" src="tau-prolog/core.js"></script>
<script type="text/javascript" src="tau-prolog/lists.js"></script>

and then either fetch an external .pl file which contains the Prolog code, or use it inline like in the examples or in react.

Carsten H
  • 831
  • 15
  • 32