0

I'm new in node js and I'm trying to parse a JSON. This is what I've done so far:

const JSON = require("nodemon/lib/utils");
...
someFunction() {
let sens = [];
// sensor is the result of fs.readFileSync call and file is a json array
sens = JSON.parse(sensors); 

It throws me:

JSON.parse is not a function.

How can I solve it?

6
  • 1
    why are you importing const JSON = require("nodemon/lib/utils");? Commented May 21, 2019 at 9:35
  • this is was suggest me by my IDE, is it wrong? Why downvote? Commented May 21, 2019 at 9:36
  • 1
    JSON.parse() is a standard JavaScript function supported by Node.js. You overwrite the JSON object in your first statement and this is why it doesn't work. Commented May 21, 2019 at 9:40
  • 3
    As an aside, don't blindly follow what your IDE tells you to do. Try to understand why it suggests something and what the consequences will be. Commented May 21, 2019 at 9:42
  • 1
    @GJCode Thanks for asking. This helped me. Commented Aug 16, 2019 at 20:08

2 Answers 2

4

Just comment out the very first line. JSON is built in already.

//const JSON = require("nodemon/lib/utils");
Sign up to request clarification or add additional context in comments.

3 Comments

Or remove it entirely, for that matter.
thanks, as I said I'm new in node so I didn't know it and nobody had my same problem in this network, so I don't know why downvote....
@GJCode Just a little tip, pretty much anything you can do in JavaScript, you can do in Node since it's built on Chrome's JavaScript engine.
0

JSON is kind of a global object. It doesn't need to be required. So, just remove the line where JSON is required and it'll work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.