0

I'm trying to use a PEGJS parser in multiple different files. I use the method var Parser = require("./Parser");

When I try to parse something using Parser.parse, though, I am unable to do so, because Parser is undefined.

How can I fix this? It had been working previously, but I made some changes to my grammar and had to regenerate the parser.

The exact error I get when running it is Uncaught TypeError: Cannot read property 'parse' of undefined

The basic code I'm trying to import is below.

module.exports = (function() {
    //Some other functions
    function parse(input) {
        //Parse code
    },
    //Some other functions
})();

And I'm trying to import this function and use it with

var Parser = require("./Parser");

//Some other code

    Parser.parse(options.queryText)
NumberOneRobot
  • 1,691
  • 6
  • 17
  • 23
  • I notice that you're requiring using './Parser'. Is the parser file in the node_modules folder? If so try removing the ./ and see if that helps. – Jephron May 28 '14 at 01:05
  • It's not in the node_modules folder. It's in the same folder as the other files that reference it using require. – NumberOneRobot May 28 '14 at 01:14
  • You should edit your question to include some code. What does Parser.js set as its module.exports? What could cause that to become null? – Jephron May 28 '14 at 01:29
  • Parser.js is exactly what pegjs outputs when it is run via "pegjs grammar.txt parser.js" at the command line. It sets module.exports equal to a function with a number of subfunctions, one of which is parse. But when I try to access that parse function, I get the undefined error, because Parser is not defined. – NumberOneRobot May 28 '14 at 01:33
  • possible duplicate of [Node.js - use of module.exports as a constructor](http://stackoverflow.com/questions/20534702/node-js-use-of-module-exports-as-a-constructor) – Paul Sweatte Oct 27 '14 at 18:49

0 Answers0