简体   繁体   中英

npm dependencies not in package.json - due to missing comments?

I am wondering, if there is a way of using comments for my package.json dependencies.

Right now we have a bigger package.json file and we get more and more lost about the dependencies and there they come from. On other languages (not javascript) we can easily add comments. But since JSON is not supporting comments, this gets really tough for us.

Is there a optional file format for package.json to define our dependencies? If not, how can we manage to create a package.json with comments?

Far more I am wondering, why node/NPM forces developers NOT to document, by choosing a fileformat which makes it impossible to document?!?

Hope someone can help us out of the dark...

I had the same problem earlier on this year. I've just solved that problem with writing some basic script like this

#!/usr/local/bin/python

import os

os.rename("package.json", "package_M.json")

with open("package_M.json") as f:
    with open("package.json", "a") as tmp_p:
        for line in f:
            if "//" not in line:
                tmp_p.write(line)
os.system('npm install')
os.remove("package.json")
os.rename("package_M.json", "package.json")

I know it something like "Hacking" but it works for me :)
Hope it will help !

I did not understand your title, why would some dependencies would fail to get installed to package.json . The only explanation would be forgotten --save flag upon npm i .

Moving onwards,

Is there a optional file format for package.json to define our dependencies?

No.

If not, how can we manage to create a package.json with comments?

See the original question that this question duplicates: How do I add comments to package.json for npm install? there are some recipes there. Personally, I don't want to put comments in package.json s, but I do use comments in my JSON's. I simply add dummy fields like "widt__comment___(value_below_is_capped_to_660_||_false_will_be_set_to_660_too)": false, . I omit the last letter and it appears on top when JSONs are sorted (next field would be width , so its comment starts with widt_ ).

why node/NPM forces developers NOT to document, by choosing a fileformat which makes it impossible to document?

package.json will have to be reliably parsed and rendered back, what limits the possible formats' choices. JSON is very reliable, old format, with very strict, known rules on its parsing and rendering. JSON is also covered by standards RFC 7159 and ECMA-404. TOML is not covered by any. YAML is not covered by any standards either. By the way, TOML is still on v0.x which is not even considered stable as per Semver.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM