npm install cannot read package.json

后端 未结 3 1960
时光说笑
时光说笑 2020-12-17 19:02

I am trying to manage my node package dependencies. I\'d like to be able to install all the required dependencies by running a command, and from what I have read, one way to

相关标签:
3条回答
  • 2020-12-17 19:36

    The only solution is to specify the exact version of the dependencies. NPM sometimes does not recognise > or .x

    0 讨论(0)
  • 2020-12-17 19:40

    npm ERR! Unexpected token ?

    In case there is no BOM, also check if you just have a "?" somewhere in the file or other errors, e.g. a missing or additional ",".

    0 讨论(0)
  • 2020-12-17 19:56

    Proper answer:

    Your editor adds a byte-order-mark to the JSON file, which makes the octet-stream an invalid JSON text.

    JSON RFC says:

    JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

    Since the first two characters of a JSON text will always be ASCII characters [RFC0020], it is possible to determine whether an octet stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking at the pattern of nulls in the first four octets.

           00 00 00 xx  UTF-32BE
           00 xx 00 xx  UTF-16BE
           xx 00 00 00  UTF-32LE
           xx 00 xx 00  UTF-16LE
           xx xx xx xx  UTF-8
    

    The bug report you mentioned has been closed for this reason.

    From my understanding, any valid ASCII encoded text also happens to be valid UTF-8, so together with the absence of the BOM it explains why it now works as expected.

    In general, I think you should set up your text editor to save files in UTF-8, without a byte-order-mark. See What's different between UTF-8 and UTF-8 without BOM? for discussion. Per What encoding is expected for Node.js source code? , Node.js would accept non-ASCII characters in JS source files encoded this way. This can be handy when you want to embed a non-ASCII string somewhere in the source code.

    0 讨论(0)
提交回复
热议问题