npm "failed to parse json" - node.js

When I'm trying to install express with npm I always get the following error:
Failed to parse json
No data, empty input at 1:1
File: /root/.npm/inherits/2.0.1/package/package.json
Failed to parse package.json data.
package.json must be actual JSON, not just JavaScript.
This is not a bug in npm.
Tell the package author to fix their package.json file. JSON.parse
What am I doing wrong?
sudo npm install -g express
OS is Ubuntu 12.04 (precise) armhf

Thanks to Jivings from this comment:
npm cache clean
solved the problem.

I had the same problem but "npm cache clean" didnt resolve it for me. I had to go back to my package.json and realize i had comma where it shouldn't i suppose as shown below:
},
"devDependencies": {
"axios": "^0.15.3",
"bootstrap-sass": "^3.3.7",
"cross-env": "^3.2.4",
"jquery": "^3.1.1",
"laravel-mix": "0.*",
"lodash": "^4.17.4",
"vue": "^2.1.10",
}
after the "vue..." so I deleted that and every went back to normal. So it is worth checking the package.json file first before running npm cache clean

Mostly, this error is due to a syntax error in package.json file.
In my case, the opening curly brace for dependencies object in package.json was missing:-
Code--------------------------------
{
"name": "psrxjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies":
"rxjs": "^5.4.3"
}
}

In Laravel project:
Delete 'node_modules' folder;
npm cache clean
npm update

I solved the issue using below steps:
Delete node_modules folder
Delete package-lock.json file
Run npm install
Run npm start

I also got the same error message while run npm install, first run npm package.json to check errors in package.json file, if not then run npm cache clean

You could get this error from not doing npm init.

I faced this problem several times before I got used to using NPM. Most off the time it was because I failed to use npm init before npm install

For me the issue was fixed by changing the name of the package
from
"name": "portfolio"
to
"name": "portfolio2"

I experienced a similar issue today after updating Node on Windows 10. My local build tasks started failing and upon investigation I saw all these errors in my dependency package.json files. None of them were valid JSON anymore and I was seeing messages like:
npm WARN Failed to parse json
npm WARN Unexpected token '\u0019' at 1:1
npm WARN ������2�����bE�;���1L �\5�e���k2?��,?;��쏏a��(T��w��+I��/�6�P} ��i�|e�
npm WARN ^
in my console.
This story has a happy ending as it turns out that new Node doesn't play nice with old NPM and updating NPM to version 5 solved the problem. Hope this helps other folks who may experience this variation on this issue.

The following bash script fixes the problem automatically
#!/usr/bin/env bash
echo -e '#!/usr/bin/env bash' > npm_install.sh
cat npm-debug.log | grep 'error File:' | sed -n 's:.*error File\: \(.*\):echo "Removing \1"\nrm -f \1\necho "Cleaning npm cache"\nnpm cache clean\necho "Reinstalling npm"\nnpm install\n./npm_reinstall.sh:p' >> npm_install.sh
chmod +x npm_install.sh
./npm_install.sh
It should be saved to npm_reinstall.sh
and granted execution permissions using
chmod +x npm_reinstall.sh
The script is preforming the following tasks:
Looking for an error File : in npm-debug.log using grep
Using sed to generate the fix commands 3-5 only if there are errors
Removing the empty file rm -f /1 = file path from the first group in regular expression .*error File: (.*)
Cleaning npm cache npm cache clean
Reinstalling npm npm install
Running ./npm_reinstall.sh recursively till no errors are found
More information about npm install can be found at npm-install command documentation

In my case
Missing a comma somewhere in a package.json Check your package.json file.
After that sudo npm install
or
To clean the cache memory.
sudo npm cache clean

In addition to Pank's answer, if you encounter this kind of error
npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected token } in JSON at position 550 while parsing near '...eact": "^7.12.4",
npm ERR! JSON.parse },
npm ERR! JSON.parse "dependencies":...'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
You need to make sure your package.json is a valid json, not a javascript.

Please check with unused white spaces inside package.json file, it might cause by extra white spaces.

I think you may be did some change in package.json and that is not valid
Delete node_modules
Delete package.json
Create a new NPM package with
npm init
install all of your package once again
npm install express

For those of you who are new to this like me, I forgot to initialize my JSON package with the npm init command.

remove any unnecessary comments, the error you referred occurs usually due to the syntax error. Or if this won't help, try to clean the cache by "npm cache clean".

1.Basically it comes for wrong placement of comma so remove comma at wrong position(esp error occurs for placing comma(,) before closing flower brace('}') in package.json so look into it. This is one solution
Run
sudo npm cache clean
sudo chown -R 1000:1000 "...path/.npm"

Don't forget to edit your package.json, escpeially the dependencies.
For example, one of my chatting room projects needs the following content in package.json:
{
"name":"chatrooms",
"version":"0.0.1",
"description":"Minimalist multi-room chat server",
"dependencies":{
"socket.io":"~0.9.6",
"mime":"~1.2.7"
}
}

Try to open your txt editor and select "plain text" for the package.json then re-save. Sometimes issue is overlooked and simple things are holding the answer.

Related

ERR Missing script critical

I am trying to generate critical css path using node module by addy osmania (https://github.com/addyosmani/critical)
i have below code in package json
"scripts": {
"criticalcss": "node criticalcss.mjs",
}
when i do npm run critical i get bellow error...
npm ERR! Missing script: "critical"
What am i doing wrong? My node installation is in c:user/admin and my production files are in xampp htdocs
i used node script.mjs and it worked
Your script is named criticalcss and you are running the command npm run critical.
Try
npm run criticalcss

NPM ERR Code E401: Unable to authenticate, need: Bearer authorization

I downloaded a NodeJS application from GitHub and facing the following error when executing npm install.
npm ERR! code E401
npm ERR! Unable to authenticate, need: Bearer authorization_uri=https://login.windows.net/c1156c2f-a3bb-4fc4-ac07-3eab96da8d10, Basic realm="https://pkgsprodeus21.pkgs.visualstudio.com/", TFS-Federated
My Node version is 6.13.1 and NPM version is 6.13.4.
Following is the content of package.json file:
{
"name": "DemoApp",
"version": "1.0.0",
"description": "A social oasis for lovers of pizza.",
"repository": "****",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "****",
"license": "MIT",
"dependencies": {
"#hapi/boom": "7.4.2",
"#hapi/catbox": "10.2.1",
"#hapi/catbox-redis": "5.0.2",
"#hapi/cookie": "10.1.0",
"#hapi/good": "8.2.0",
"#hapi/good-squeeze": "5.2.0",
"#hapi/hapi": "18.3.1",
"#hapi/inert": "5.2.1",
"#hapi/joi": "15.1.0",
"#hapi/vision": "5.5.2",
"aws-sdk": "2.488.0",
"bcryptjs": "2.4.3",
"bootflat": "2.0.4",
"fs-extra": "8.1.0",
"handlebars": "4.1.2",
"lodash": "4.17.13",
"pg": "7.11.0",
"sequelize": "5.9.4"
}
}
I have been stuck at this issue since yesterday and still no luck finding the solution.
Any help would be highly appreciated.
This is what worked for me.
First, delete the .npmrc file in your Users folder. This folder:
C:\Users\[your user name]
Then run this command in your project folder that has an .npmrc file in it:
npx vsts-npm-auth -config .npmrc
Use npm install --registry https://registry.npmjs.org instead of npm install
No need to delete the .npmrc file, the following worked for me
npm logout
Then
vsts-npm-auth -config .npmrc
If you get E401 with a private npm registry after upgrading to npm v7, remove your package-lock.json and reinstall.
The registry url setting in .npmrc needs to match the http/https protocol in your package-lock.json exactly.
Or as Stuart pointed out: find and replace to update the existing lock file with the correct URL
Worked for me:
Delete the yarn.lock/package-lock.json files
npm install
I had the same issue, my discovery was as follows:
The application node.js version was 14.0
but the node version in my machine was 16.0.
I had to install the node the version 14.0 to resolve the issue.
To manage multiple node versions this tool is highly recommended.
windows - Nvm for windows
Linux - Nvm for Linux
I had this exactly same error and turned out it was an issue with personal access token (PAT). Renew your PAT and run vsts-npm-auth.
Delete old .npmrc file from user home directory and then run the following command
vsts-npm-auth -config .npmrc -T $HOME/.npmrc
This issue comes from a wrong configuration in your .npmrc file. I had a slightly different error so I'm sharing it here.
In my case the exact error was:
Unable to authenticate, need: Bearer realm="<registry>", Basic realm="<registry>"
My npmrc file should connect to a private npm registry and looked like this:
registry=https://<private-registry>
//<private-registry>:_authToken=<token>
//<private-registry>:always-auth=true
The issue was that I needed to add the https:// protocol also to the second and third line and it worked. In the end it looked like this:
registry=https://<private-registry>
//https://<private-registry>:_authToken=<token>
//https://<private-registry>:always-auth=true
I solved it running this command:
npm logout/npm login
I had the same error with our company registry configured in .npmrc
registry=https:<compnay-registry-url>
Node version : 16.10.0
NPM version : 7.24.0
Solution:
Execute npm login
$ npm login
npm notice Log in on https:<registry-url>
Username: xxxx
Password:
Email: (this IS public) (xxxx)
Logged in as xxx on https:<registry-url>.
After this .npmrc got updated with
//<registry-url>/:_authToken=xxxxx
If you are trying to install any package from your private repo and you are getting this error with npm i package_name command then
1. remove your package-lock.json from the the directory where to tried to create the package from
2. reinstall dependencies: npm i
this will resolve the issue.
I had the same issue caused by an expired token in the .npmrc file in my project directory. Sadly the error is very unspecific on what causes the auth error.
This worked for me. I just logged in again in NPM and the problem was solved
npm login
After u write your username and password execute again
npm install
For me the the problem was in .npmrc file the key password needed to be changed to _password then npm install worked.
Using a Mac and Azure - it was because my Personal Access Token (PAT) had expired and it didn't let me know.
Open your .npmrc with your auth tokens in.
Go to your azure repo and click your profile and then personal access tokens.
Create a new token.
Open terminal and run:
node -e "require('readline') .createInterface({input:process.stdin,output:process.stdout,historySize:0}) .question('PAT> ',p => { b64=Buffer.from(p.trim()).toString('base64');console.log(b64);process.exit(); })"
Paste your PAT in and press Enter
Copy the Base64 encoded value into your .npmrc password.
Had the same error.
In my case, the initial project including package-lock.json file was composed with NodeJS version 12 and npm version 6.
But trying to run npm install on my local machine with NodeJS version 16 and npm version 7 was causing this problem.
My solution was installing dependencies with the initial version of NodeJS. I used docker to get the correct version, run this command from the root directory of your project (where the package.json file is located):
docker run --rm -it \
-w /app \
-v $PWD/:/app \
node:12 \
npm install
If you do not have docker installed, you can try using nvm.

npm errors and warnings in terminal

can anybody tells me what are those errors about and how to fix them?
Everytime i try to install something in node with npm these errors show up in terminalenter image description here
ENOENT stands for basically "Error, No Entry". this means it was looking for the package.json file and it couldn't find it.The fields mentioned below are also not found because they are a part of the package.json file
so create a package.json file in the current current directory using
npm init
and add the required content in the required fields
i would also recommend you to install the modules locally into the directory of the particular project using
npm install express --save
Hope my answer helps,
cheers
The error you are facing is because you do not have package.json file. Npm installs the package in a node_modules/ subfolder, but warns you that there is no package.json file. If you want to manage localy installed npm packages you should create a package.json file. Start by creating an empty folder:
$ mkdir myapp
$ cd myapp
and then create a new package.json executing
$ npm init
Answer (or skip) all questions and at the end a brand new package.json will be created.
You can get more information from the Getting started articles in npm documentation: Working with package.json

sh: 1: node: Permission denied

Tried to run this command on ubuntu 18.04
npm install -g pngquant-bin
but I got this error,
[..................] | fetchMetadata: sill resolveWithNewModule npm-conf#1.1.3 checking installable status
npm WARN deprecated gulp-util#3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
/root/.nvm/versions/node/v10.8.0/bin/pngquant -> /root/.nvm/versions/node/v10.8.0/lib/node_modules/pngquant-bin/cli.js
> pngquant-bin#5.0.0 postinstall /root/.nvm/versions/node/v10.8.0/lib/node_modules/pngquant-bin
> node lib/install.js
sh: 1: node: Permission denied
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! pngquant-bin#5.0.0 postinstall: `node lib/install.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the pngquant-bin#5.0.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-08-12T18_08_02_197Z-debug.log
Do you do you know how to deal with this?
I tried every solution found in this articles yet not succeeded.
Got the same error sh: 1: node: Permission denied
So this worked for me
npm config set user 0
npm config set unsafe-perm true
These issues happen because of broken packages. Go to the main folder. If using Linux use command
sudo rm -rf node_modules.
After that run this command if you are using yarn
yarn install
If you are using npm run this command
npm install
Delete the node_modules and install it again
sudo rm -rf node_modules
npm install
in fact, npm can't use root account to install anything. if you use root account, npm will create a non-permission account to install. in this case, if the package need to execute writeFile or other operation which need permission, the error node: Permission denied will be raised.
so, you can choose optional arbitrary under:
npm install xxx --unsafe-perm
npm config set unsafe-perm true
create high-permission account dedicate to execute npm install
In my case it was a silly typo, I was forgotten to add node into the front of the start command in package.json. So I've changed:
"scripts": {
"start": "app/server.js"
}
... to:
"scripts": {
"start": "node app/server.js"
}
The /root/.npm/... log path in your original message shows you're already running as root, and (despite what others advise) I'd say this is most likely causing your problem.
My (limited) experience running Node as root is that most npm install runs get quite a long way, but then fail with some variation on the error you showed. The only reliable solution I've found is to not run Node or npm as root at all on Ubuntu. Just use a normal user account to download and unpack the Node installation.
At least one problem with running as root for me turned out to be because some dependency-of-a-dependency npm install script was calling setuid to switch to a less-privileged user. For some reason, it chose UID 500—which doesn't exist on Ubuntu—and consequently lost all its privileges. The 'Permission denied' errors were therefore because I was running as root; setuid doesn't work for a normal user.
I believe this is related to Error: setuid user id does not exist npm ERR! when npm install forever -g.
Solved my problem chmod -R a+x node_modules
As far as my understanding goes the os is blocking your ability to execute commands described in node_modules so by my understanding what this command does is say everything in node_modules is okay to execute.
By the time I found the solution it was 4 AM, so I didn't really bother to figure out what I actually did. If someone knows what -R a+x node_modules does exactly feel free to drop it in the commands and I will make an edit.
I make the chown to project user owner (in USERID) dir and resolv the "permission denied" problem:
sudo chown -R USERID.USERID *
Additionally (and this might be useful for docker) you can override this configuration setting globally via the environment variable npm_config_user -- for example:
ENV npm_config_user=root
I ran into the same error an nothing really helped. I found a medium article explaining how to set up an angular build management. For some reason adding
- npm link #angular/cli#13.2.5
to my build script made it. I basically added all of the recommendations above. So my build script now looks like this
- ...
- npm config set user 0
- npm config set unsafe-perm true
- npm i --force
- npm link #angular/cli#13.2.5
- ...
I hope it helps! I would be happy if someone could explain why it actually worked.
This is an old question but maybe someone still need some help.
This errors often is displayed because you have defined in the package.json just the path. For example:
{
// more definitions
"scripts": {
// other scripts
"getPax8Products": "<filepath>",
// more scripts
},
// more definitions
}
In this case, you need to:
Add the following lines in the very beggining of the script
#!/usr/bin/env node
'use strict';
Give the file execution permission
# in UNIX based
chmod +x <filepath>
You also can modify the package.json and add the node command. However, you need to be aware that NPM will run in the script's directory:
{
// more definitions
"scripts": {
// other scripts
"getPax8Products": "node <filepath>",
// more scripts
},
// more definitions
}
For Deploying with Docker:
make sure /node_modules is deleted or added to dockerignorefile
make sure /dist is deleted or added to dockerignorefile
the problem was solved for me by deleting both files
and build them in the container
For me, I had not installed my dependencies. node_modules did not exist, but I had jest installed globally apparently. Running npm ci and then running npm test solved my issue.
npm install lite-server --save-dev
packages.json:
"scripts": {
"dev": "lite-server",
},
"devDependencies": {
"lite-server": "^2.6.1"
}
npm run dev
I stuck with same issue when tried to install packages into AWS Sagemaker instance
The issue coming because NPM by default install new global packages into ~/.npm-global
When you run npm install -g by root, npm is try to install package into /root/.npm-global/..., and stuck with access denied.
Simply workaround to re-config global folder for npm.
(https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally)
Here is example of install obj2gltf
mkdir /npm-global
npm config set prefix '/npm-global'
export PATH=/npm-global/bin:$PATH
npm install -g obj2gltf
I had that error too and tried the above solutions without any change. My error was caused because I had Windows (11) with a WSL and NVM installed on both operating systems. I had to uninstall NVM on my Windows to resolve the conflicts.
I think if you develop in your WSL and have a resource installed on both operating systems, a dependency might point to the wrong operating system with the resource (in my case to the NVM on Windows). The WSL user didn't have sufficient permissions to perform any execution on the Windows machine, which lead to the error.
you need root user permission, just add sudo keyword before the command and write your password
sudo npm install -g pngquant-bin
Try to install node at project folder
npm install node

Node-sass is not recognized by command line

I'm trying to set up node-sass, following the instructions on CSS-Tricks. Node and npm are installed correctly, and the node-sass installation worked too. When I go to run node-sass --output-style compressed -o dist/css src/scss, though, I get an error message stating
'node-sass' is not recognized as an internal or external command,
operable program or batch file.
I've done a fair bit of Googling and searched Stack Overflow directly. My question isn't about "node" not being recognised as a command. I know node is working as I can run node -v and npm -v, and node-sass was successfully installed after running npm install --save-dev node-sass (there's a folder in node_modules) and no errors appeared in the command line.
Other information: I am running Windows 10 and just did a clean install of node and npm before trying to use node-sass.
EDIT: I uninstalled and reinstalled with -g thanks to #Bhavik's suggestion, and it's now working
You need to install it globally
npm install -g node-sass
Or add it in package.json
"devDependencies": {
"node-sass": "4.5.0"
},
"scripts" : {
"node-sass": "node-sass --output-style compressed -o dist/css src/scss"
}
And then do
1. npm i, which in this case would be similar to npm install --save-dev node-sass
2. npm run node-sass
Reference: npm scripts, npm-run-scripts
You can simply run this code
npm install -g sass
sass --watch sass:css
Hopefully work
npm commands check "node_package" folder and try to run things there. You can try
npx run scss
to install scss and then run it, even if it is not installed before.
The below solves the problem
yarn global add node-sass-chokidar
node-sass v4.13+
Install node-sass in your project locally
cd <root path of your project>
yarn add -D node-sass
// or
npm install -D node-sass
Add a script to your package.json
"scripts" : {
...
"compile:sass": "node-sass --recursive --watch <sass directory> --output <css directory>",
...
}
Run the script from the command line
yarn compile:sass
// or
npm run compile:sass
This is a simple problem don't worry too much. Just go to package.json file and add this code
"devDependencies": {
"node-sass": "4.9.2"
},
"scripts" : {
"node-sass": "node-sass --output-style compressed -o dist/css/ scss --recursive"
}
and just save the file.
And run this command,
npm run node-sass
That's all
First, run npm install -g node-sass as others have pointed out.
Now, the target of the command (sass.cmd) is not located in the current working directory. For it to still be able to run, its location must be in your PATH (or Path) environment variable.
For me, the path is: C:\Users\Guy\AppData\Roaming\npm
Make sure to restart any terminal/IDE you were trying to run it in before trying again. Otherwise it won't recognize the new environment variable.

Resources