How do I fix my npm that seems to be infinitely looping? - node.js

I was trying to change my npm and npm-cache folders to d:\\npm-global and d:\\npm-cache
I managed to do a npm config set cache d:\npm-cache
When I tried to do a npm config set prefix d:\npm-global I accidentally put a ' in the folder string as I was hitting enter. I deleted the errant folder after trying a npm i -g gulp and to my horror, npm stopped working altogether, it just sits there with my command prompt rocking up to 50% CPU usage.
Help please! I've tried searching the registry and other places, even uninstalled and re-installed the nodejs msi package on Windows. This reset the D:\Program Files\nodejs\node_modules\npm\npmrc file.
Still no luck. I can't get npm to work again. :(
edit A little digging in and I realized there is a second node server running via Adobe Creative Cloud. I doubt this is the problem though as when I run npm from the cmd.exe prompt, the correct (d:\program files\nodejs) version of the npm server is executed for that process.
edit so only sometimes when I hit Control-C, I'll get this error:
seems like it's trying to find a file that doesn't exist. However, I cleared out the AppData\Roaming\npm folder and the reinstalls reset the default d:\program files\nodejs\....npm\npmrc file. I also made sure that the environment variable NODE_PATH is not set. It wasn't set when it was working. I set it when I was trying to make my changes.

You should be able to modify your config settings manually by modifying ~/.npmrc

#jakemingolla was right so I marked that answer as accepted (thanks Jake), but I also asked this in the NPM forum and did a little test after I got it working. I thought this might help someone: https://npm.community/t/i-killed-my-npm-on-windows-trying-to-move-some-folders-reinstalling-nodejs-isnt-resetting-the-problem/5743/8?u=chi11ax

Related

Why does my computer keep forgetting node and npm installation?

My computer keeps forgetting that I have node installed despite the fact that all of the files are still there. So far, I have tried installing through the node installer on the nodejs website, using brew, using nvm, and also several of the suggestions in this gist. No matter what I try, within a couple of hours or a couple of days, I am working away and then I am suddenly throwing node errors, and when I check node -v or npm -v, I get the error command not found: node or command not found: npm. The only thing that I can figure out to get node working again is to uninstall whatever I most recently tried and then to reinstall.
It seems like I might be having some sort of env or path issue, but I don't know where to begin to start troubleshooting anything like that.
EDIT
Based on the suggestion below, I added these to my .bash_profile and it seems to have fixed my issue. Not sure why the install wasn't adding them here or in .bashrc, but adding it manually fixed the issue:
export PATH="$HOME/.npm-packages/bin:$PATH"
export PATH="$HOME/.node_modules/bin:$PATH"
maybe you forgot to add node & npm binaries to your path that's why when you open a new instance of your terminal it says that it doesn't know node and npm
Simply add the line that register the binaries into your path within your .profile file that might be located in your home directory

How to include the path for the node binary npm was executed with

Windows, VSC, Running npm start got this
npm WARN lifecycle The node binary used for scripts is C:\Program
Files\nodejs\node.exe but npm is using
C:\somewhere\AppData\Roaming\npm\node_modules\node\bin\node.exe
itself. Use the --scripts-prepend-node-path option to include the
path for the node binary npm was executed with.
I understand it means my local version is diff from the one in the PATH variable (C:\Program Files...). How do proceed to tell it to use --scripts-prepend-node-path?
I played a trick by replacing the C:\Program Files\nodejs with C:\somewhere\AppData\Roaming\npm\node_modules\node\bin in PATH variable, it does pick up that new node.exe got but there is no node binary in the current PATH. Again recommend to use the --scrip
ts-prepend-node-path option to include the path for the node binary npm was executed with
Like I said, replacing the actual path in PATH system variable didn't fix the problem completely, it still complained about binary is missing. Found this solved the there is no node binary in the current PATH problem. So I restored the original PATH, then:
Simply create a file at the root folder of the app, called .npmrc,
place this line into it:
scripts-prepend-node-path=true
Here's another way that works: npm config set scripts-prepend-node-path auto
Conflict between your node binary and your npm
In case the error looks something like:
npm is trying to use the same node as the one it use to run itself.
resolve this conflict by adding the node directory to your PATH:
npm config set scripts-prepend-node-path true
From the npm documentation:
npm run sets the NODE environment variable to the node executable with
which npm is executed. Also, if the --scripts-prepend-node-path is
passed, the directory within which node resides is added to the PATH
Having this issue in Visual Studio 2017, I instead told VS to always use the NodeJS that I had installed from nodejs.org rather than the frozen-in-time one that ships with Visual Studio. (The one that shipped with VS2015 is so ancient it doesn't really work anymore.)
In Visual Studio, go to TOOLS > OPTIONS > search for EXTERNAL WEB TOOLS > and ensure "C:\Program Files\nodejs" (or wherever nodejs.org installed it) is first in the list of paths, adding it if necessary.
For WebStorm, invalidating caches and restarting worked for me.

const INLINE = 'inline'; with npm related commands

Today I was trying to work on Angular2 (template https://akveo.github.io). As per installation guide, I ran following commands.
I have installed Node.js v6.9.1.
npm install --Worked fine.
npm server --Failed with below error.
E:\........\node_modules\script-ext-html-webpack-plugin\index.js:3
const INLINE = 'inline';
^^^^^
SyntaxError: Use of const in strict mode.
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
After many random searches and failing to resolve it, I decided to think about it with a calm head. To me it looks like it is a problem with ES2015/ES6. Package script-ext-html-webpack-plugin uses const variable which is a ES2016 feature. However system is unable to resolve it.
Also this problem can come with any package like hapi, selinium etc. But everything should have same solution I guess.
I have tried many related commands on the installation guide of the website mentioned above but nothing works and ends with same error. I have also many have faced same issue online but nothing concrete is visible as answer.
Could anyone help me out?
Update
Below approach has also fixed many of my colleague's machine who were facing some random errors with nodejs.
This problem is resolved in at least my machine. I am certainly not a person who understand nodejs completely but got it working after some googling. The solution given below may be common to many other issues I guess.
The system I am working has Windows 10 as Operating System and has 64 bit processor.
Go to control panel and uninstall nodejs (just to make sure you do not have traces of any previous install).
Install latest version of nodejs from their website (I installed v6.9.1 for 64 bit processor).
Go to Environment variables for system (just type environmental variables in start menu if using Windows 10) and edit the Path. Delete ‘C:/Program Files (x86)/nodejs’ (as I had installed 64 bit nodejs). This ensures when using command you do not use any other previously installed nodejs.
Start command prompt in Admin mode and navigate to your project and run npm install.
In case you get any/some error, then run npm info graceful-fs -v or npm info graceful-fs whichever runs in your machine.
Then run npm update -g npm.
Then run npm install again and this time it should work.
Run npm start and everything should work fine.
On running website, sometimes we see any other issue and that can be seen from console logs. But good thing is, console logs also tell you the problem. In most cases its related to 32 bit and 64 bit processor due to our recent changes. In my case, I was facing issue with SASS configuration due to 32/64 bit machine and console log asked me to run npm rebuild node-sass to fix the issue.
Go back to command prompt, hit Ctrl+C to stop npm server. Run node rebuild node-sass. Run npm start again and everything should be working.
Hope it helps.

'npm install' extremely slow on Windows

For me is npm install extremely slow. I'm using Windows 8.1 with the latest npm version. My connection speed is around 100Mbit/s.
The project I'm trying to install has around 20 packages/dependencies and it takes around 30 Minutes to install all dependencies ...
Does anyone have a clue?
I ran into the same problem, using --verbose as peterh mentioned showed me the source of the problem: I am behind a proxy, which uses its own certificates for https-connections.
According to user "Mletter1" on https://github.com/npm/npm/issues/8872 the issue is quite easily solved by using http:
npm config set registry http://registry.npmjs.org/ --global
And voilà, it's fast again. Of course you should only do this, if you're ok with cleartext npm infos over the net ;-)
Warning regarding security: this is an insecure approach if you don't use any other means of antivirus protection.
From eternity → to 5 seconds builds:
search for Windows Defender settings
→ Virus & threat protection
→ Virus & threat protection Settings
→ Exclusions
here add this two paths to your list of excluded realtime protection folders:
C:\Program Files\nodejs
C:\Users\<your username>\AppData\Roaming\npm
PS: replace the <your username> with your respective User folder name; or use alias to the AppData folder like: %AppData%\npm
If you're wondering why you can't find the AppData folder in Explorer, make sure to check "View" options for "Show hidden folders".
I know its just a workaround, but try using the build-in "Windows PowerShell"
I've been facing the same issue for a while. I was trying out the following
npm i typescript live-server --save-dev
The install would get stuck at it forever. However adding the -verbose flag worked just fine.
It seems like npm do not deal well with internet connections and can sometimes end in such situation.
1) I'll advice you to check if your firewall or antivirus is not performing any scan or filtering which may slow down npm install.
2) You may also use caching from npm like below.
npm install package-name --cache "c:\path\to\use\as\dependencies\cache"
this will allow npm download and cache dependencies in the directory you specified.
3) another solution for caching npm packages dependencies is npm lazy, you may find more information about this at npm Lazy website
Note: the caching process may or may not improve the download speed but it will probably save downloads time when installing or updating your npm packages.
For me it appeared to be a problem with IPv6.
Disabling IPv6 helped immensely for me.
With IPv6 turned on "update-package newtonsoft.json" took 1:45 to time out with the error "update-package : A task was canceled."
With IPv6 disabled "update-package newtonsoft.json" completed in 10 seconds
Turning off Windows Firewall and Windows Defender Real-time protection did it for me in Windows 10.
Fully turning off virus and firewall protection isn't the safest option. While it makes things faster, it leaves you more vulnerable.
In my case, I only excluded the node.exe process in virus & threat protection.
In your start menu, type "windows security" and open the app.
Click on "Virus & threat protection"
Virus & threat protection settings => Manage settings
Exclusions => Add or remove exclusions
Add an exclusion => process => type node.exe => click Add
just a simple create-react-app went down from 2m40s to 1m10s
npm commands are also executed by node.exe, so the same improvements are noticed there.
I faced the same issue while creating project while using angular cli "ng new PROJECT_NAME" this command triggers "npm -install" after creation of project, which took too long to install, and got stuck at "Installing packages for tooling via npm" . Follow these which worked for me
1) Check your npm version "npm --version" (must be higher than 2.1.8)Update your npm package by "npm -g install --save latest-version"
2) Remove the progress bar which consumes more time "npm set progress=false"
check this issues if still doesn't fix your problem
https://github.com/nodejs/node/issues/4150
https://github.com/npm/npm/issues/11283
Hopefully this sorts the problem Cheers!
In my case it was due to over 200 vulnerabilities. When I ran
npm audit fix
that solved only just a few of them. Eventually, I had to run
npm audit fix --force
and it got way better than before.
I tried all the config's (proxy, registry, etc) nothing worked for me, so did a complete uninstall and install node & npm. Which helped to solve the issue.
Turning off the Windows Defender Firewall helped for me.
Because it just doesnt feel right I first added the WIFI to a Private Network (via WIFI settings) and only turned off the Windows Defender for a Private Network instead of all public Networks.
The next step should be: what network traffic causes this and how to add this to the Windows Defender exception rule :)
Make sure you really are using a recent version of npm. In my case C:\Program Files\nodejs\npm.cmd had been using an old npm-cli.js below the default prefix folder %appdata%\npm rather than a newer npm-cli.js below C:\Program Files\nodejs.
Setting the environment variable npm_config_prefix to C:\Program Files\nodejs solved that. (Before finding that solution, I tried using the --prefix argument and found out the hard way doing so "forces non-global commands to run in the specified folder".)
I found a huge performance increase going from 3.10.9 to 6.13.4-- enough to make npm install times acceptable despite interference from antivirus.
I just went through a rabbit hole trying to identify the issue. If npm install keeps getting bottlenecked you should heavily consider modifying the properties of your virtual ethernet and disable all of the offload items.
I had 0 proxies or caching issues with npm.
See here for the tutorial on how to get to the menu and disable your offload settings. Make sure you do this for both your your ethernet and VEthernet and you should immediately notice a difference. I went from a 10 minute hanging resolution that sometimes just stopped down to 10 seconds installs.
https://www.dincloud.com/blog/tuning-windows-get-15x-speeds-on-large-file-copies-today
in addition to all other great comments, I'd also suggest switching to yarn (https://marketplace.visualstudio.com/items?itemName=geeklearningio.gl-vsts-tasks-yarn).
using devops (selfhosted agent v.2.192.0), with node (v.14.17.6), we were able to trim total build (install + build) from ~10min to ~6-7 min.
This is highly situational but we have a local registry (npm config get registry) that was not available (VPN:d) in when I was running the npm install ... and this obviously made it very slow but it still succeeded because everything was still available through cache.

nodejs module.js:340 error: cannot find module

I installed nodejs in C:\Program Files (x86)\nodejs
then I created a .js file and saved it in my desktop just to output 'hello world' in the console:
console.log('hello world');
When I tried to run the file from the command prompt:
C:\Users\Laura>cd desktop
C:\Users\Laura\Desktop>node nodeTest.js
I get:
module.js:340
throw err;
^
Error: Cannot find module 'C:\Users\Laura\Desktop\testNode.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
I read many other related questions, and some of them recommend to run the install, and so I did.
C:\Users\Laura>npm install -g express
But no luck, still getting the same error message.
EDIT: This answer is outdated. With things like Yarn and NPM 5's lockfiles it is now easier to ensure you're dependencies are correct on platforms like Heroku
I had a similar issue related to node_modules being modified somehow locally but the change was not reflect on Heroku, causing my app to crash. It's relatively easy fix if this is your issue:
# Remove node_modules
rm -fr node_modules
# Reinstall packages
npm i
# Commit changes
git add node_modules
git commit -m 'Fix node_modules dependencies.'
git push heroku master
Hope that helps for others with a similar issue.
I was having the same problem with a server someone had written a while back. It turns out I didn't have a few things installed that the script required.
This question was helpful to me.
I ended up being able to use:
npm install yourMissingDependency
Although in your instance it may just be a file that it doesn't have a path to.
You could try putting that file in the same folder you are running the node script from.
I think the issue is the NODE_PATH environment variable. I had the same problem but once I issued (on Windows)
set NODE_PATH=C:\Users\MyUser\node_modules
then all worked ok.
Cheers,
Restart your command prompt and check your path variable (type: path).
If you can't find find nodejs installation dir from output add it to the path variable and remember to restart cdm again...
I had a nearly identical issue, turned out my JS file wasn't actually in the folder I was calling it from, and I had gone one folder too deep.
I went up one directory, ran the file, it recognized it, happily ever after.
Alternatively, if you go one folder up, and it gives you the same error, but about a different module, take that same file in your parent folder and move it into the subfolder you were previously trying to run things from.
TL;DR- your file or its module(s) is not in the folder you think it is. Go up one level
Try typing this into the Node command-line environment:
.load c:/users/laura/desktop/nodeTest.js.
It should work for what you're trying to do.
If you want to call the file directly, you'd have to have it in the root directory where your Node installation resides.
Hi fellow Phonegap/Cordova/Ionic developers,
I solved this issue by doing the following
1. C: drive -> Users -> "username" eg. john -> AppData -> Roaming
2. Inside the "Roaming" folder you need to delete both "npm" and "npm-cache"
folder.
3. Now build your project, and it should work
Happy coding!!!
I faced the same problem. I just copied the testNode.js file(that contain the test code) and pasted into the root of nodejs directory manually. I tried this command C:\Program Files (x86)\nodejs>node testnode.js
Bingo! I received this message.
Then I typed this url in a browser and received the message "Hello World".
Hope this help somebody.
You need to:
Remove the tick of Hide extensions for known file types from Windows Explorer folders option
Your file will appear as testNode.js.txt
Remove the trailing .txt so as the file to be resolved as JS file
That's it, now it works!
I had the same problem then I found that I wasn´t hitting the node server command in the proper directory where the server.js is located.
Hope this helps.
Try npm install --production and then npm start.
Easy way for this problem
npm link e
I also got this issue and this was due to wrong path that we mention while running. Check your file path and also make sure that there is no space between the name of your directory name.
Try npm start in Node.js Command Prompt.
Look at the end of the messages - it gives you the path of log file in "Additional Logging Details ..." something like c:\users\MyUser\npm-debug.log
Open this file in Notepad and find the real address of Node.exe :
something like C:\\Program Files\\nodejs\\\\node.exe
Try cd to this path
Call node.exe + <full path to your server file.js>
Server is listening on port 1337 !
I had the same problem, but finally I tried copying the Node_modules folder in the same project folder, and it worked.
I executed following command and it works for me.
PM> npm install ee-first ee-first#1.1.0 node_modules\ee-first
While i tried to run my first node application i had encountered the same issue but when i tried to see what the issue might be it was the path of the file. I was running the node .js from different location and hence encountered the issue. When i fixed the path this worked fine.
In my case, i got this error because i was just in the wrong directory. So node couldnt find the module that i wanted to run. Just be sure your node file is in your Desktop directory.
If you are using a framework like express, you need to put the package.json file into the folder you are using and don't forget change main name.
Remove the fs extra from the path -
path: 'D:\\FrontEnd Dev\\Git projects\\Appname-master\\node_modules\\webpack-manifest-plugin\\node_modules\\fs-extra\\package.json',
requestPath: 'fs-extra'
And run the command npm i
Make sure you saved the file as JavaScript. Un check 'Hide extensions for all known type' check box in Folder Options window will show you the correct file extension(Folder>>view>>Option).
Faced the same problem while trying to run node-red.
node <directory structure where js is located>/red. js
In my case it was :
node AppData/Roaming/npm/node_modules/node-red/red.js

Resources