npm install <node_module> --save not updating dependencies - javascript

<=== To anyone experiencing the same issue reinstalling the computer works ===>
I've had to reinstall my computer, had to reinstall Node and NPM and don't know what's gone wrong and it's driving me crazy.
I'm on a Windows 10 64-bit computer, I run npm init and it works fine. I can install dependencies with no trouble, both locally and globally, but using npm install <node_module> --save doesn't update my package.json file with dependencies.
I've reinstalled both npm and node, no change.
I've tried running cmd as administrator, nothing.
I've restarted the computer too, still nothing.
If anyone knows how to solve this, please help. I'm desperate!
Update #1: when installing some modules I see that they create .cmd files in my project folder, if this is a clue, haven't experienced that before
Update #2:
Just created a fresh install using npm init and then tried npm install express --save and this is the package.json file:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

I fixed this by completely wiping and reinstalling my computer. If there's another fix to this I don't know it, but this is one fix... The nuclear way.

Just a suggestion, you may try to add "sudo" before your command.

Related

Can my NPM CLI package be executed on CMD without installing globally?

I have written an NPM package which has its own CLI commands.
Let's name that package as xyz and imagine it's now avaialable on npmjs.com
So, let's say a user installs this package in his project by running npm install xyz.
And now he wants to run a CLI command provided by xyz package on his terminal in his project.
xyz do_this
Can this be done without installing this package globally by user ? Or without any further configuration for the user ?
Here's some part of the package.json of the xyz package.
{
"name": "xyz",
"version": "1.0.0",
"description": "Description",
"main": "index.js",
"preferGlobal": true,
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
........
Here's how npm works. When you install a package in the local directory, it installs the executable dependencies files inside node_modules inside the folder with package.json, but if you use --global, it places it globally where the user has set their path.
For example, when I run npm install http-server, my executable ends up as ./node_modules/http-server/bin/http-server but when i install it globally, I have it as node_modules/http-server/bin
The work around for this is, if you want to just run the executable, just execute it inside like so ./node_modules/http-server/bin/http-server. If you want it as a command, you'll need too have the user add the directory to their Path so when the user enters the command, the computer looks for the command inside that folder.
Here's a guide to adding PATH directories in Linux, you'll just add the directory /pathtofolder/node_modules/http-server/bin/ or whatever your folder is. https://linuxize.com/post/how-to-add-directory-to-path-in-linux/
For example, if I wanted to add http-server from my local folder to my path, I would run
export PATH="/pathtofolder/node_modules/http-server/bin/:$PATH"
Good luck! Let me know how I can help you!

Error installing package using npm instead of yarn

I have created git repository which will be used as npm package in other project. Lets say that sharable repository name is genesis-service-broker.
I am using this shareable repository inside one of the service(activation service). In this project, I am installing package using yarn. Its running perfectly fine here.
"dependencies": {
...
"genesis-service-broker": "git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis",
...
}
When I am trying to use genesis-service-broker package inside onother service(partner service) its not able to install the dependencies. In this project, I am installing dependencies using npm. If I install dependencies using yarn its working perfectly fine.
I am not getting any errors in npm install command. I am just not able to find genesis-service-broker folder inside node_modules, when I am installing dependencies using npm.
package.json file inside genesis-service-broker repository. (for reference purposes)
{
"name": "service-broker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git#git.my_project.com:amol.barewar/service-broker.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"config": "^3.2.5",
"form-data": "^3.0.0",
"node-fetch": "^2.6.0",
"request": "^2.88.0",
"uuid": "^3.4.0"
}
}
there is a difference in behaviour here, between yarn and npm
yarn add retains the name of the git project in dependencies, and creates a folder with the same name in node_modules.
So, yarn add git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis will lead to a module getting installed as node_modules/genesis-service-broker
On the other side, npm install gets the name from the name property in package.json; and it will lead to module getting added as node_modules/service-broker in your case... and also the dependencies map will be like
"dependencies": {
...
"service-broker": "git+https://${key}:x-oauth-basic#git.my_project.com/${user}/genesis-service-broker.git#create_service_broker_for_genesis",
...
}
Because of this difference, the requires(...) might fail.
As, in this case, with yarn that module will be available through -
require('genesis-service-broker')
And for npm through -
require('service-broker')
So, all in all, it will help to keep the name property in package.json same as the project name.

Running npm run compile:sass returns error: npm ERR! missing script: compile:sass

The sass folders and files are in the correct place. What is wrong?
I have the package.json which I created, with this code in there.
{
"name": "starter",
"version": "1.0.0",
"description": "starter file",
"main": "index.js",
"scripts": {
"compile:sass": "node-sass sass/main.scss css/style.css -w"
},
"author": "Nez",
"license": "ISC",
"devDependencies": {
"node-sass": "^4.12.0"
}
}
As you can see the script is called compile sass when I try to run it it keeps giving the error
npm ERR! missing script: compile:sass
I have the sass compiler installed already as a dev dependency
I had this happen as well. The only way I could fix it was to move my files out of my dropbox folder on an external hard drive and move them to my main computers' hard drive. I think it has something to do with Dropbox, but I'm not sure.
One of your sass files has a syntax error
eg.
file 1 has
background: %pink
the %pink value is undefined you should check your files if %pink* is implemented
I had the same issue and tried everything until I simply saved the project files within my code editor and then all of a sudden it worked.
Add this to your code all you need to do is add npx before your node
I.e. npx node...
Then go to your terminal to that directory and run this command npm run compile:sass
Hope this works...because it worked for me will work for you too
I think you should check your file path
After 3 years!
for me it's solved by rewriting the package.json "scripts" line :
"compile:sass": "node-sass sass/main.scss css/style.css -w"
Go to package.json
Add below lines
"scripts": {
"sass": "sass sass/main.scss css/style.css",
"test": "echo \"Error: no test specified\" && exit 1"
},
For compilation use command:
npm run sass
Note: I have used main.scss as source file name

missing start script in NPM

First, sorry for my bad english.
I'm trying to deploy an app to Heroku using the command heroku local web in terminal, but I'm getting ERR! missing script: start, even though I have the script in my json file. And if I try to run npm start in terminal the same error appears. I searched for similar problems in Stack Overflow but I couldn't solve.
My .json is:
{
"name": "server",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
.
.
My dependencies here
.
.
},
"scripts": {
"start": "node index.js",
"test": "echo \"Error: nko test specified\" && exit 1"
}
}
PS.: When I run npm install in terminal, the json file just erases the script and everything I wrote, for example I added "config" : { "port" : "3000" }, but after running npm install it just disappeared.
I think you confused yarn and npm
If you want to use yarn and if you package.json contain a key like start you can call yarn start
But, if you want to work with npm you need to use npm run start the run it's important because npm can't understand.

Not able to install "node_module"s

I am a node.js beginner..!!
I am trying to install node_modules but getting following error.
I am on Windows and using node.js v5.7.0
Also, getting the same error on Mac OS X.
First this is not an error but a warning saying you don't have a package.json file. This file could look like this for a beginning.
{
"name": "project name",
"version": "0.0.1",
"private": true,
"main": "main.js",
"start": "node main.js",
"dependencies": {
"colors": "^1.1.2"
}
}
Check this to why using a package.json file.
Edit
You can also run npm init (suggested by #Nicklas Ridewing)
You need to create a package.json file before you run npm install colors
Just run
npm init
And follow the guide to create one :)
Use npm init to setup you project package.json.
Use npm install <package-name> --save to save it to your dependencies.
Use npm install <package-name> --save-dev to save it to your development dependencies.
Those will be saved in to your package.json which will be your project main configuration file.

Categories