1

I have actually a problem with antlr. I use angular in visual studio code. I know how to include and write a grammar in a project. However, now I faced the problem that an error occurs while starting it:

"ERROR in ./node_modules/antlr4/CharStreams.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\simon\antlrTestProject\node_modules\antlr4' ERROR in ./node_modules/antlr4/FileStream.js Module not found: Error: Can't resolve 'fs' in 'C:\Users\simon\antlrTestProject\node_modules\antlr4'"

I found out that fs is no longer available in angular 6+. Unfortunetly I found no possibility to solve this.

Simon
  • 354
  • 1
  • 5
  • 15
  • Does this answer your question? [Antlr javascript with webpack](https://stackoverflow.com/questions/41618709/antlr-javascript-with-webpack) – ggorlen Mar 25 '20 at 00:32

2 Answers2

2

In order to use the ANTLR JavaScript runtime outside of node.js, you need to configure webpack to exclude certain modules that exist in node.js, but not in the browser. From the docs:

in the webpack.config file, exclude node.js only modules using: node: { module: "empty", net: "empty", fs: "empty" }

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Unfortunately I am not familiar with webpack. I also found in some discussions that: "node: { module: "empty", net: "empty", fs: "empty" }" is no longer possible. Is there maybe another solution? – Simon Feb 19 '19 at 13:40
  • @Simon Can you link to those discussions? – sepp2k Feb 19 '19 at 14:49
  • This is the link: https://stackoverflow.com/questions/50202948/update-to-angular-v6-module-not-found-error-cant-resolve-fs – Simon Feb 19 '19 at 17:18
  • However I found a much simpler solution. If this problem occurs you can add to your package.json file the following: "browser": { "fs": false, "path": false, "os": false } – Simon Feb 19 '19 at 17:20
0

If the suggested solutions didn't work, there's a really not-recommended one that works for me if it's a angular CLI project .

Go to node_modules / @angular-devkit / build-angular / src /angular-cli-files /models / webpack-configs / browser.js

There's a json with a key-value:

node: null

Change that to :

node: { fs: "empty" }

That's obviously a horrible solution because :

  • You have to make that change each time you update that package ;
  • Your team has to be aware of that change and also apply it

If there were a way to force that change, it'd be a more viable solution

Tim Meehan
  • 160
  • 2
  • 8