问题
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.
回答1:
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" }
回答2:
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
来源:https://stackoverflow.com/questions/54754585/how-to-use-antlr-if-an-error-with-fs-occurs