ERROr: **npm ERR! Unexpected token < in JSON at position 0 while parsing near '<!DOCTYPE HTML PUBLI...'** - npm

Image I am cloning the sitecore Habitat project to my local machine.
After cloning while running npm install, I am getting this error. I tired in all possible ways from the stack overflow answers, but did not succeed with answers still its showing errors, if anyone known about this please let me know.
ERROr: **npm ERR! Unexpected token < in JSON at position 0 while parsing near '<!DOCTYPE HTML PUBLI...'**

Just delete
package-lock.json file
It works for me.
I getting error like:

I found this solution in another question and worked for me after I set the registry like this:
npm set registry https://registry.npmjs.org/

Setting npm Registery worked for me
npm set registry https://registry.npmjs.org/

This error means there is a proxy in the middle which is denying you access to the Internet.
The error comes from the HTML page that is rendered from the proxy.
You need to check with your network administration to either allow the connection through (an exception), or you have to configure the variable HTTP_PROXY in your environment, which is like this:
http://username:password#proxy.server.com:port

Having tested npm install in several repos with the same error I ran npm cache clean --force and tried again. This is not as severe a solution as deleting all traces of node and reinstalling from the ground up.
I use an npm registry hosted at work for internal npm packages so couldn't change the registry source.

yes , its happned due to proxy setting, if this problem occurs, please check your proxy setting, in my case npm was getting blocked, You need to check with your network administration to either allow the connection through (an exception), or you have to configure the variable HTTP_PROXY in your environment

You need to check all required npm config setup like:
https-proxy=https://username:password#accessdomain:port/
proxy=http://username:password#accessdomain:port
registry=https://registry.npmjs.org/
//If required also add
msbuild_path=C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\MSBuild.exe

I was getting the same issue. I had un-installed NodeJs and deleted folders npm and npm-cache from Users\username\Appdata\Roaming folder and installed it.
Then set config registry by using command terminal as.
npm config set registry https://registry.npmjs.org/

To give an actual description of the problem and solution:
This happens when the URL set as your npm registry is incorrect (or as others have mentioned, a proxy has got in the way). Either way, you end up trying to download a package from an endpoint displaying HTML, which npm fails to parse as JSON (as one would expect).
You can view the URL for your npm config with
npm config get registry, and set it with npm config set registry https://my.registry.com/ (or https://registry.npmjs.org/ for the public registry)

I tried everything mentioned here. Nothing worked. Then I moved jsonwebtoken dependency to the bottom of package.json ( Below my registy specific dependencies ).
Then it worked!
Spent 6 hours in fixing this. I feel so small now.

Related

AsyncApi Change npm registry url

Is there are command-line switch to point the npm registry to different url? I have created a template and published to our company's registry. When referring the package, it is referring to https://registry.npmjs.org/. I did install the template locally using
C:\>npm install -g #company/asyncapi-csharp-template
C:\>ag DomainServiceA.yml #company/asyncapi-csharp-template -o ouput
and the error response is
Something went wrong:
HttpErrorGeneral: 404 Not Found - GET
https://registry.npmjs.org/#company%2fasyncapi-csharp-template - Not found
Tbh best if you submit issue here https://github.com/asyncapi/generator/issues as normally it should work.
also try to add --debug flag to understand more what is happening with template usage in generator

NPM fails any command silently. How to change npm config manually?

I was trying to set a certificate for npm (npm config set cafile) when it suddenly gave up entirely and any command would just result in two empty lines and then nothing:
Since nothing works I can't try to unset my certificate to see if that helps.
Can I change the npm settings somewhere manually? Alternatively, how can I reset all npm settings on my system?
I've tried to uninstall and re-install node+npm but it still behaves the same.
I found the faulty setting in C:\Users{User}.npmrc, and after I fixed that npm came back to life.

Error 403 - Forbidden while trying npm install

So just like every day I do different projects and install additional modules, today I want to install another module and such an error, reinstall the server and still the same .... [Error]The error appeared overnight
I found the issue from here: https://github.com/npm/npm/issues/2119#issuecomment-5321857
It's a very common issue in npm. Actually npm is not working good with HTTPS over a proxy. Changing the registry URL from HTTPS to HTTP will fix the problem.
Try this out:
npm config set registry http://registry.npmjs.org

npm install throws unexpected token error

In my repo directory, when I try npm install, I'm getting this error: Unexpected token < in JSON at position 3035. I also get the same error when trying npm uninstall [package]. Any ideas what might be causing this? I'm on the latest version of npm.
Check if the npm registry used is still valid and you can access it. One place the npm registry information is stored is ~/.npmrc. If you are not sure if the registry specified in .npmrc is valid, you may remove the registry information from this file (please ensure others don't need it if this is a shared system) and npm will use the default registry.

Nexus npm private registry configuration

I am using Nexus Repository ManagerOSS 3.0.2-0, I have successfully created npm hosted repository, but when I click on the link it always says 400 bad request.
And also through command line am not able to perform npm adduser pointing to this registry .
Any idea what am doing wrong?
The link in the UI is non functional, it's there to copy for use in setting up tooling, the 400 is expected behavior at this point in time.
As for npm adduser, I believe this should work. Make sure to have the npm Bearer Token Realm enabled.
You can read more about enabling it here: https://books.sonatype.com/nexus-book/reference3/npm.html#_authentication_using_realm_and_login