Search code examples
javascriptnode.jsjsontypescriptjson5

Requiring a JSON with comments in node.js


If you use typescript you can init a default tsconfig.json, and that json will have javascript // and /* */ comments in it. I've encountered a situation with ts-jest where I need to require my tsconfig and parse it, but jest is not parsing it because json doesn't allow comments. see this I'm not sure how typescript handles it, but it seems to deviate from the rule.

// this fails if tsconfig has comments
const tsconfig = require('./tsconfig')

I want to keep the comments because they are really helpful to understand and maintain my tsconfig.json, and I want to require my config to be able to avoid duplicating code and make things more dynamic.

Is there a way to require a json file with comments using nodejs?


Solution

  • json5 can do the trick. Install JSON5 and then:

    const JSON5 = require('json5');
    const tsconfig = JSON5.parse(jsontxt);
    

    Note that removing comments by yourself is possible but has several pitfalls, and that's why it's not recommended. You should look for:

    • Strings may contain sequences that look like comments, only they are not.
    • Comments may contain sequences that look like string beginning, only they are not.
    • Strings may contain " that don't mark string end such as "foo\"bar". It's more confusing with "foo\\", where the second " does end the string.
    • A // comment may contain /*, but that does not start a multi line comment.
    • A // in a multi line comment does not start a single-line comment, such that removing comments in /* // */ "hi" results with "hi".
    • Does a \ continue a single-line to the next line? Does a \*/ end a multi line comment, or does the \ make the * act differently? (Neither are supported in JavaScript).
    • are multi line strings supported (they are in JavaScript)? How are they declared/dealt with.