npm always using home directory as current working directory - node.js

npm insists on using my home directory as the current working directory, no matter which directory I am executing commands from.
Happens with both cmd and Powershell, I've tried removing and reinstalling node, both using the installer on nodejs.org, and via Chocolatey with the same result.
If I navigate to e.g. C:\test and run npm init, the wizard places the resulting package.json file in my home folder.
If I run npm install <package> from any folder, the package is installed to C:\Users\myusername\node_modules
Trying to install a package globally also fails, with this
npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\ProgramData\\chocolatey\\lib\\nodejs.commandline.0.10.33\\tools\\node.exe" "C:\\ProgramData\\chocolatey\\lib\\npm.1.4.9\\tools\\node_modules\\npm\\bin\\npm-cli.js" "install" "gulp" "-g"
npm ERR! cwd C:\Users\<myusername>
If I navigate to a folder, start the node REPL and run process.cwd() the directory I started node from is returned, so the issue seems to be limited to npm.
Running npm get gives this result:
C:\test> npm get
; cli configs
registry = "https://registry.npmjs.org/"
user-agent = "npm/1.4.9 node/v0.10.33 win32 x64"
; node bin location = C:\ProgramData\chocolatey\lib\nodejs.commandline.0.10.33\tools\node.exe
; cwd = C:\Users\<myusername>
; HOME = C:\Users\<myusername>
; 'npm config ls -l' to show all defaults.
I've searched quite a bit for a solution, and have found some similar issues, but none that solved my problem. I know that npm walks up the directory tree to look for a node_modules folder, but this happens in folders outside my home directory as well.
Any tips on what I try to resolve this?

Turns out I had a cd C:\Users\<myusername> command in HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun in the registry. Meaning that this command was executed any time CMD.EXE started.
No idea how that got in there, I might have added it myself at some point.
Anyway, if someone experiences differences between the current working directory when executing a script and when running the same command manually in an open command prompt, HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun and HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRunare good places to check.

Not 100% sure but I think you'll get this if when you don't have a package.json file. Try to create one (npm init) then try installing any modules. Should then save them under node_modules of whatever current directory you're in.

Related

How does NPM resolve & run scripts from package-json?

For curiosity's sake, I want to create a folder in node_modules and be able to run it as a script in package.json's "scripts", but I have not been able to find out how to.
I've noticed with other package.json scripts like "react-scripts" that they wont work in the command line, but will when NPM runs them. How does npm resolve scripts?
Does it have to be a legit package for NPM to run it? (Like, will it check against the NPM registry for if that package exists, or contains scripts?)
If a module has a ["bin" entry in its package.json]1, then npm will put the executable script specified by that "bin" entry into a .bin folder in node_modules.
When running a script with npm run, npm will put that node_modules/.bin directory first in the PATH so they are found with npm run but not when run directly from the shell.

why npm is not responding when using the keyword npm in command prompt

I'm trying to use npm intsall, but it doesn't respond anything
even npm -v is not working, node and npm is installed.
It stays still like this.
I have installed node
Click here
Path for installed node js
C:\Program Files\nodejs
click here to view path image for node on my machine
Here is the new solution . Try out this .
In the home directory, there was an entry of .npmrc, and this was probably creating a conflict with the default configuration of the npm inside node. By removing .npmrc from user home did the trick.
Note: For windows this file will be inside user folder.
You can find the location of your personal .npmrc using the following command
STEP-1) - npm config get userconfig
On Windows it may look like: C:\Users\pushprajsinh.npmrc
(Replacing pushprajsinh with your username)
Step-2) - Once you get the path of .nprc file , remove that file. (Note:- it might be hidden)
Try out this solution
Check if your node/npm is installed in a directory with blank spaces in the name. This cause problems in Windows.
Reason may be- proxy .
check what is the output of
npm install --verbose

npm start error nodejs

I give the command npm start. Terminal responds:
Make sure you are in correct directory when you run npm install, your current directory should have package.json file. for example based on the project you uploaded, you should be inside diplom folder and then run npm install.

Error while using create-react-app - ENOENT

New Node & React user here. I'm following the React tutorial but run into a problem on my Windows 10 machine:
C:\Users\Wout>create-react-app my-app
Creating a new React app in C:\Users\Wout\my-app.
Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts...
npm ERR! path C:\Users\Wout\my-app\node_modules\abab
npm ERR! code ENOENT
npm ERR! errno -4058
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename 'C:\Users\Wout\my-app\node_modules\abab' -> 'C:\Users\Wout\my-app\node_modules\.abab.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Wout\AppData\Roaming\npm-cache\_logs\2018-03-14T15_21_11_867Z-debug.log
Aborting installation.
npm install --save --save-exact --loglevel error react react-dom react-scripts has failed.
Deleting generated file... node_modules
Deleting generated file... package.json
Deleting my-app / from C:\Users\Wout
Done.
Things I've tried so far:
Reinstall Node.js (v8.10.0, npm 5.6.0)
Disabling Adobe creative cloud sync & related processes (these were spawning node.exe processes)
Running CMD in admin
Run the command from VS Code Powershell
Closing Visual Studio Code before executing the command
Run the command with npx
Rebooted the system several times
Running the command from the user folder as well as other drives
Running the Typescript version: create-react-app my-app --scripts-version=react-scripts-ts
It's all quite strange to me, since on Mac OS X the command executes without issues. I also can't seem to find other people with the same problem.
For what it's worth, it always stops after this "finalizing abab" package step.
I have an installation of XAMPP running an Apache and MySQL service, don't know if that has anything to do with it. I don't think so since I'm not even running the app yet, plus the server runs on port 3000 anyway.
I eventually solved it by closing as many extra processes as possible. Will try to find out which process was interfering with the command.
Edit: Ding ding ding! It was MalwareBytes! The "realtime protection 30-day trial" had restarted after an update and it was screwing with the filesystem.
I also just installed MalWareBytes, and I get the same error. I tried running create-react-app from the CLI as administrator, and while it was running I read your solution, and so I shut down MalWareBytes as the installation was in progress.
It worked, but I don't know if that is because I ran as administrator, or because I shut down MalWareBytes.
But for anyone having this problem, you could also try running your command prompt/powershell with administrator rights.
try running the command from the project directory...
worked for me.
i ran the command from the parent of the project directory for example:
reactApp/hellowworld
run from helloworle directory instead...
In case you have just install the create-react-app command , try to run the command from a new terminal . (re-open another tab).

npm not reading .npmrc file

I'm trying to install a library from a private repository, and I keep getting an error in trying to use npm.
I'm using:
OSX Mavericks 10.9.3
Node v0.10.28
npm 1.4.10 (this was installed after trying with 1.4.13 and it still not working)
I am running this from my home directory, and the ~/.npmrc file is in the directory.
whenever I run the command: npm install 'library name here'
I get the following error:
npm http GET https://registry.npmjs.org/dslib-js
npm http 404 https://registry.npmjs.org/dslib-js
npm ERR! 404 404 Not Found: dslib-js
npm ERR! 404
npm ERR! 404 'dslib-js' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
npm ERR! 404
npm ERR! 404 Maybe try 'npm search dslib'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, or http url, or git url.
npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "dslib-js"
npm ERR! cwd /Users/marcos.pedreiro
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.10
npm ERR! code E404
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/marcos.pedreiro/npm-debug.log
npm ERR! not ok code 0
Edits:
When I run ls -a (in the home directory) this is the output:
. .ssh Music
.. .subversion Pictures
.CFUserTextEncoding .vagrant.d Public
.DS_Store Applications VirtualBox VMs
.Trash Desktop clients
.bash_history Documents dev
.gradle Downloads npm-debug.log
.matlab Google Drive ~:.npmrc
.npm Library ~:.npmrc.template
.npmrc.bak Movies
This is the output of the command npm config ls -l | grep config
; cli configs
globalconfig = "/usr/local/etc/npmrc"
userconfig = "/Users/marcos.pedreiro/.npmrc"
:End Edits
Any help would be greatly appreciated. Thanks!
In my case the character encoding was wrong for some reason, I had to convert it to UTF-8.
I used "npm get" to discover the problem, the result was full of u/2411... strings.
If what you posted is truly the output of ls -a, then the filename is wrong.
~:.npmrc should be named .npmrc.
The ~ at the beginning suggests you used a shell shortcut to place the file in your home directory (maybe something like cp .npmrc ~:.npmrc. The trouble is likely the colon. ~/ will be interpreted as "my home directory", but ~something/ will be interpreted as the home directory of the user named something. Since there is no user something, the system is probably just treated ~: as literal text.
Try renaming the file and see if that works:
mv "~:.npmrc" .npmrc
For people running Windows, check if Windows is hiding file extensions. For me, Windows was saying that the file name was .npmrc, but the real file name was .npmrc.txt.
Removing the .txt extension fixed the issue.
I'm assuming the Node package dslib-js is contained within your private npm repository. You'll need to set that repository in your local .npmrc file. For example, if your repository was at http://myrepo.com:4000 you would run the following command:
$ npm set registry "http://myrepo.com:4000"
The error your getting above is because the package dslib-js is not available on the npmjs.org, as you can see from visiting this URL: https://www.npmjs.org/package/dslib-js. By default (without a registry set in your .npmrc file), npm will search https://registry.npmjs.org (which can be browsed on the web via the URL I pasted above).
In case it helps anyone landing here: I had a npm-shrinkwrap.json file in the project root that was overriding the registry config from .npmrc. Removing the shrinkwrap file solved the issue.
I missed a couple of things to download my packages published in my private repository.
First The file name was .npmrc.txt instead of .npmrc. In windows, you can't create file name directly. So I opened command prompt then fired
echo "auth_token" > .npmrc
Second I was just pasting my auth_token in .npmrc file. You have to append the registry address also.
//**.**.visualstudio.com/_packaging/**/npm/registry/:_authToken=token_here
In case this helps anyone... my issue was I created the .npmrc file in a text editor so npm was ignoring the file because of the permissions on it. In order to fix it I deleted the file. Then I remade the file through the command line.
Cheers!
In case anyone has the same issue as me. We have a private registry but before adding the .npmrc file I tried running npm install. This created the package-lock.json and it was overriding the new registry that was added in the .npmrc file.
Simply deleting the package-lock.json and re-running npm install fixed it for me
I came across a scenario where npm ci was not picking/reading configs from .npmrc file.
Replacing it with npm install resolved the issue.
Node version 10.18.0
Slightly different reason of npm not picking up the .npmrc. But after changing the registry setting in my project's .npmrc file I had to delete previously existing node_modules folder and package-lock.json, then install with npm install --save. Doesn't make much sense, but after this, the package-lock.json file (re)created was using the correct registry.
I had a same problem mentioned in the question, I have resolved the problme in the following way
step 1: I went to the user directory in the c drive
C:\Users\user1
step 2: Found text.npmrc file which in the text format
step 3: I just replaced the file text.npmrc to .npmrc
step 4: after run npm install it working fine as i expected

Resources