admin管理员组

文章数量:1279113

If you use typescript you can init a default tsconfig.json, and that json will have javascript // and /* */ ments 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 ments. see this I'm not sure how typescript handles it, but it seems to deviate from the rule.

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

I want to keep the ments 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 ments using nodejs?

If you use typescript you can init a default tsconfig.json, and that json will have javascript // and /* */ ments 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 ments. see this I'm not sure how typescript handles it, but it seems to deviate from the rule.

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

I want to keep the ments 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 ments using nodejs?

Share Improve this question edited Nov 18, 2020 at 13:17 Tom Hale 47k40 gold badges201 silver badges265 bronze badges asked May 25, 2020 at 5:17 ThomThom 5511 gold badge6 silver badges12 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 9

json5 can do the trick. Install JSON5 and then:

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

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

  • Strings may contain sequences that look like ments, 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 // ment may contain /*, but that does not start a multi line ment.
  • A // in a multi line ment does not start a single-line ment, such that removing ments in /* // */ "hi" results with "hi".
  • Does a \ continue a single-line to the next line? Does a \*/ end a multi line ment, 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.

require-json5 is purpose-built for this:

const requireJSON5 = require('require-json5');
const tsconfig = requireJSON5('./tsconfig.json');

Or even more simply:

require('require-json5').replace();
var tsconfig = require("./tsconfig");

I managed to do it by pre-processing the json to strip the ments. The following code is doing the job for me:

const fs = require('fs')
const stripForwardSlashForwardSlashComment = new RegExp('//(.*)', 'g')
const stripForwardSlashStarComment = new RegExp('[/][*](.*)[*][/]', 'gm')
let jsontxt = fs.readFileSync('./tsconfig.json', 'utf8')
jsontxt = jsontxt.replace(stripForwardSlashForwardSlashComment,'').replace(stripForwardSlashStarComment, '')
const tsconfig = JSON.parse(jsontxt)

Self answer note: Posting to keep this public and get some feedback

本文标签: javascriptRequiring a JSON with comments in nodejsStack Overflow