Miscellaneous

Resolving a ‘SyntaxError: Unexpected token j in JSON at position 0’ Error in an Azure Pipeline

March 2, 2021

We use Azure Pipelines to build our projects when deploying changes to various environments. Most of the time, they run without a hitch. But, as the laws of the universe dictate, the first day that the guy who set them up went on vacation they started failing with the error “SyntaxError: Unexpected token j in JSON at position 0”. This resulted in me having to pull up my sleeves and dig into the issue.

First off, lets look at the full error log:

2021-03-02T16:44:00.0324391Z ##[section]Starting: npm install
2021-03-02T16:44:00.0458900Z ==============================================================================
2021-03-02T16:44:00.0459221Z Task         : npm
2021-03-02T16:44:00.0459638Z Description  : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
2021-03-02T16:44:00.0460031Z Version      : 1.182.0
2021-03-02T16:44:00.0460398Z Author       : Microsoft Corporation
2021-03-02T16:44:00.0461661Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/package/npm
2021-03-02T16:44:00.0462048Z ==============================================================================
2021-03-02T16:44:00.7892391Z [command]C:\windows\system32\cmd.exe /D /S /C "C:\hostedtoolcache\windows\node\6.17.1\x64\npm.cmd --version"
2021-03-02T16:44:01.9610194Z 3.10.10
2021-03-02T16:44:04.4978427Z [command]C:\windows\system32\cmd.exe /D /S /C "C:\hostedtoolcache\windows\node\6.17.1\x64\npm.cmd config list"
2021-03-02T16:44:05.7023877Z ; cli configs
2021-03-02T16:44:05.7024350Z user-agent = "npm/3.10.10 node/v6.17.1 win32 x64"
2021-03-02T16:44:05.7024542Z 
2021-03-02T16:44:05.7024832Z ; environment configs
2021-03-02T16:44:05.7025150Z prefix = "C:\\npm\\prefix"
2021-03-02T16:44:05.7025523Z userconfig = "D:\\a\\1\\npm\\38470.npmrc"
2021-03-02T16:44:05.7025700Z 
2021-03-02T16:44:05.7026014Z ; globalconfig C:\npm\prefix\etc\npmrc
2021-03-02T16:44:05.7026362Z cache = "C:\\npm\\cache"
2021-03-02T16:44:05.7026541Z 
2021-03-02T16:44:05.7026921Z ; node bin location = C:\hostedtoolcache\windows\node\6.17.1\x64\node.exe
2021-03-02T16:44:05.7027312Z ; cwd = D:\a\1\s
2021-03-02T16:44:05.7027655Z ; HOME = C:\Users\VssAdministrator
2021-03-02T16:44:05.7028025Z ; "npm config ls -l" to show all defaults.
2021-03-02T16:44:05.7028195Z 
2021-03-02T16:44:05.7029744Z [command]C:\windows\system32\cmd.exe /D /S /C "C:\hostedtoolcache\windows\node\6.17.1\x64\npm.cmd install"
2021-03-02T16:44:07.1354144Z npm ERR! Windows_NT 10.0.14393
2021-03-02T16:44:07.1354891Z npm ERR! argv "C:\\hostedtoolcache\\windows\\node\\6.17.1\\x64\\node.exe" "C:\\hostedtoolcache\\windows\\node\\6.17.1\\x64\\node_modules\\npm\\bin\\npm-cli.js" "install"
2021-03-02T16:44:07.1355464Z npm ERR! node v6.17.1
2021-03-02T16:44:07.1355749Z npm ERR! npm  v3.10.10
2021-03-02T16:44:07.1355877Z 
2021-03-02T16:44:07.1356183Z npm ERR! Unexpected token j in JSON at position 0
2021-03-02T16:44:07.1356678Z npm ERR! 
2021-03-02T16:44:07.1357061Z npm ERR! If you need help, you may report this error at:
2021-03-02T16:44:07.1357521Z npm ERR!     <https://github.com/npm/npm/issues>
2021-03-02T16:44:07.1357710Z 
2021-03-02T16:44:07.1358089Z npm ERR! Please include the following file with any support request:
2021-03-02T16:44:07.1358549Z npm ERR!     D:\a\1\s\npm-debug.log
2021-03-02T16:44:07.2037725Z Found npm debug log, make sure the path matches with the one in npm's output: D:\a\1\s\npm-debug.log
2021-03-02T16:44:07.2038268Z 0 info it worked if it ends with ok
2021-03-02T16:44:07.2038750Z 1 verbose cli [ 'C:\\hostedtoolcache\\windows\\node\\6.17.1\\x64\\node.exe',
2021-03-02T16:44:07.2039310Z 1 verbose cli   'C:\\hostedtoolcache\\windows\\node\\6.17.1\\x64\\node_modules\\npm\\bin\\npm-cli.js',
2021-03-02T16:44:07.2039784Z 1 verbose cli   'install' ]
2021-03-02T16:44:07.2040120Z 2 info using npm@3.10.10
2021-03-02T16:44:07.2040439Z 3 info using node@v6.17.1
2021-03-02T16:44:07.2040785Z 4 silly loadCurrentTree Starting
2021-03-02T16:44:07.2041132Z 5 silly install loadCurrentTree
2021-03-02T16:44:07.2041493Z 6 silly install readLocalPackageData
2021-03-02T16:44:07.2041853Z 7 silly rollbackFailedOptional Starting
2021-03-02T16:44:07.2042235Z 8 silly rollbackFailedOptional Finishing
2021-03-02T16:44:07.2042600Z 9 silly runTopLevelLifecycles Finishing
2021-03-02T16:44:07.2042996Z 10 silly install printInstalled
2021-03-02T16:44:07.2043430Z 11 verbose stack SyntaxError: Unexpected token j in JSON at position 0
2021-03-02T16:44:07.2043868Z 11 verbose stack     at Object.parse (native)
2021-03-02T16:44:07.2046437Z 11 verbose stack     at module.exports (C:\hostedtoolcache\windows\node\6.17.1\x64\node_modules\npm\lib\utils\parse-json.js:3:15)
2021-03-02T16:44:07.2047234Z 11 verbose stack     at C:\hostedtoolcache\windows\node\6.17.1\x64\node_modules\npm\lib\install.js:616:47
2021-03-02T16:44:07.2048001Z 11 verbose stack     at C:\hostedtoolcache\windows\node\6.17.1\x64\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:78:16
2021-03-02T16:44:07.2048831Z 11 verbose stack     at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:447:3)
2021-03-02T16:44:07.2049291Z 12 verbose cwd D:\a\1\s
2021-03-02T16:44:07.2049638Z 13 error Windows_NT 10.0.14393
2021-03-02T16:44:07.2050251Z 14 error argv "C:\\hostedtoolcache\\windows\\node\\6.17.1\\x64\\node.exe" "C:\\hostedtoolcache\\windows\\node\\6.17.1\\x64\\node_modules\\npm\\bin\\npm-cli.js" "install"
2021-03-02T16:44:07.2050885Z 15 error node v6.17.1
2021-03-02T16:44:07.2051207Z 16 error npm  v3.10.10
2021-03-02T16:44:07.2051569Z 17 error Unexpected token j in JSON at position 0
2021-03-02T16:44:07.2052025Z 18 error If you need help, you may report this error at:
2021-03-02T16:44:07.2052473Z 18 error     <https://github.com/npm/npm/issues>
2021-03-02T16:44:07.2052886Z 19 verbose exit [ 1, true ]
2021-03-02T16:44:07.2053035Z 
2021-03-02T16:44:07.2054977Z ##[error]Error: Npm failed with return code: 1
2021-03-02T16:44:07.2059339Z ##[section]Finishing: npm install

As you can probably determine, the pipeline was failing when attempting to execute an ‘npm install’ command.

The first thing I did was review the git history on the packages.json file to ensure no one had made any modifications, there were none. The next thing I did was review the pipeline configuration’s history to see if anyone had made any recent changes, they hadn’t.

From there I ran the pipeline again using the master branch, and it ran successfully. Since the only difference would have been the code it was checking out this told me it had to be something from a recent commit that was causing it to fail.

So I then had the pleasure of reviewing every single commit to the development branch since the last successful run. Eventually I found the culprit: someone had checked in package-lock.json and pushed it out.

Now despite the fact that the documentation clearly states that this file should be committed to source control removing it from the branch resolved the problem and allowed me to run the pipeline again. So make of that what you will.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.