Skip to content

ng update - Migration failed: Unexpected token in JSON at position 0 #27300

@deefault

Description

@deefault

Command

update

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Command: ng update @angular/core@15 @angular/cli@15 @angular/material@15 --allow-dirty --verbose
Output:
Repository is not clean. Update changes will be mixed with pre-existing changes. Locating potential npmrc files: Trying 'C:\Users\alex.s\.npmrc'...found. The installed Angular CLI version is outdated. Installing a temporary Angular CLI versioned 15.2.10 to perform the update. ✔ Packages successfully installed. Repository is not clean. Update changes will be mixed with pre-existing changes. Using package manager: npm Collecting installed dependencies... Found 61 dependencies. NPM 7+ detected -- enabling force option for package installation Fetching dependency metadata from registry... Locating potential npmrc files: Trying 'C:\Users\user\.npmrc'...found. × Migration failed: Unexpected token in JSON at position 0 See "C:\Users\user\AppData\Local\Temp\ng-bDvaNK\angular-errors.log" for further details.

[error] SyntaxError: Unexpected token  in JSON at position 0 at JSON.parse (<anonymous>) at _buildPackageInfo (C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-sMhQUt\node_modules\@angular\cli\src\commands\update\schematic\index.js:430:30) at C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-sMhQUt\node_modules\@angular\cli\src\commands\update\schematic\index.js:710:53 at Map.forEach (<anonymous>) at C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-sMhQUt\node_modules\@angular\cli\src\commands\update\schematic\index.js:709:27 at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async callRuleAsync (C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-sMhQUt\node_modules\@angular-devkit\schematics\src\rules\call.js:78:18)

The issue happens because of some package's package.json file has encoding different from UTF-8. In my case that was a package with UTF-8 with BOM encoding.

Resolution

Find that package (possibly by trying all), open package.json file from that package in node_modules and re-save it with UTF-8.

Maybe it is fixed in future versions. Creating issue with resolution, so maybe it will help someone struggling

Minimal Reproduction

  1. Assuming you have Angular 14 project
  2. Add some package to you package.json with UTF-8 with BOM encoding package.json. Or modify some package.json manually in node_modules
  3. run command ng update @angular/core@15 @angular/cli@15 @angular/material@15 --allow-dirty --verbose

Exception or Error

[error] SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at _buildPackageInfo (C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-RIewm4\node_modules\@angular\cli\src\commands\update\schematic\index.js:430:30)
    at C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-RIewm4\node_modules\@angular\cli\src\commands\update\schematic\index.js:710:53
    at Map.forEach (<anonymous>)
    at C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-RIewm4\node_modules\@angular\cli\src\commands\update\schematic\index.js:709:27
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async callRuleAsync (C:\Users\alex.s\AppData\Local\Temp\angular-cli-packages-RIewm4\node_modules\@angular-devkit\schematics\src\rules\call.js:78:18)

Your Environment

Mine environment is below (but as this is update, it is using cli 15.2.10)


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 12.2.18
Node: 16.20.2 (Unsupported)
Package Manager: npm 8.19.4
OS: win32 x64

Angular: undefined
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1202.18 (cli-only)
@angular-devkit/core         12.2.18 (cli-only)
@angular-devkit/schematics   12.2.18 (cli-only)
@schematics/angular          12.2.18 (cli-only)

Anything else relevant?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions