npm not installing in the correct directory - node.js

C:\Users\User>cd onedrive/desktop/folder
C:\Users\User\OneDrive\Desktop\folder>npm i node-superfetch
up to date, audited 23 packages in 1s
8 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
C:\Users\User\OneDrive\Desktop\folder>dir
Volume in drive C is OS
Volume Serial Number is 8E2E-B533
Directory of C:\Users\User\OneDrive\Desktop\folder
15/11/2021 10:49 AM <DIR> .
15/11/2021 11:03 AM <DIR> ..
15/11/2021 10:45 AM 0 data.json
15/11/2021 10:50 AM 92 index.js
2 File(s) 92 bytes
2 Dir(s) 395,067,858,944 bytes free
I wanted to install a npm package in the folder directory and it does install it but I have no idea where it installed it to. I just reinstalled nodejs and the issue isn't gone. I typed npm config get prefix and this is what it shows:
C:\Users\User\OneDrive\Desktop\folder>npm config get prefix
C:\Users\User\AppData\Roaming\npm

To see where npm will install, run npm root.
One possibility is that you have global set to true in a relevant .npmrc file. If so, npm root will report the global installation directory rather than the local node_modules. If you can't find where global is being set to true, you can use --no-global on the command with npm install so that it ignores the setting and installs things in the local node_modules instead. You can also set it to false in the project .npmrc which will override it being set in .npmrc files elsewhere.

Related

Can I install packages from the node_modules directory?

So a friend has sent me his node_modules directory but I don't want to manually type npm i *package* for each package and I don't have the package.json file to npm install from.
Is there a possible way to install the packages in the node_modules directory via terminal?
It is a very backwards scenario, I'm just curious if there's a way...
You can running:
npm ls -json > tree.json
node -e "console.log(Object.entries(require('./tree.json').dependencies).map(([m, v])=>m+'#'+v.version))"
It will print
[ 'fastify#2.13.0', 'fastify-url-data#2.4.0', 'pino-pretty#3.6.1' ]
for my test

Set directory for NPM

I'm working with git-bash on win 7. I want to develop an ember project and I am working on a thumb drive. I have installed node on my E drive and added the path to the env vars.
$ npm install -g ember-cli
C:\Users\me\AppData\Roaming\npm\ember -> C:\Users\me\AppData\Roaming\npm\node_modules\ember-cli\bin\ember
C:\Users\me\AppData\Roaming\npm\� -> C:\Users\me\AppData\Roaming\npm\node_modules\ember-cli\bin\ember
+ ember-cli#2.16.2
updated 553 packages in 89.25s
$ which npm
/e/nodejs/npm
$ which node
/e/nodejs/node
$ ember build
sh.exe": ember: command not found
So I think whats happening is that npm is installing the packages on the c drive. I need to set a folder on my e drive, to make this portable. How can I do this?
You can set this by configuring the npm prefix setting:
npm config set prefix e:\some\path\on\e
This could be done globally or on a per-project basis by modifying the project's .npmrc file.
Go to the folder with your package.json and run npm config ls to see your effective npm config.
See the npm docs for global settings

NPM installing to ~/.npm directory

I have to run npm install --prefix ./ --save bootstrap jquery for it to save in the node_modules. I expect from what I've read in tutorials and such this is not the expected behavior. When I just run npm install --save bootstrap it puts the files into /home/philip/.npm directory as /home/philip/.npm/bootstrap.
npm root gives the correct [project]/node_modules directory.
NPM Version: 3.10.10
Node Version: 6.10.3
OS: Ubuntu 17.04
Edit: Forgot to ask the question, how do I ensure npm install defaults to the project's node_modules directory?
It might be because global flag is set to true somewhere in npm config or via an environment variable.
To check current value run: npm config ls -l | grep global. To change it try to add global=false to your ~/.npmrc file. Also, check the value of $NPM_CONFIG_GLOBAL, it has higher priority than .npmrc.

npm install in a specific local folder

I'm currently trying to execute the command npm install to get all dependencies and modules necessary to run my package.json.
The problem is that I don't have Internet access to fetch from the internet, so I have downloaded the node_modules on a different PC and copy-paste it on my local folder that contains all. If I tried to run npm install without arguments, it's still trying to fetch from internet and fails.
I have read their documentation and apparently they have listed few npm install that takes different arguments, but still, I'm unable to install from the folder already downloaded.
I've tried to do npm install node_modules on the path that contains the package.json, but nothing. I'm running on windows 7.
If someone has an approach to specifying the local node_modules and just install all modules inside, I'll appreciate.
Thanks!
You probably should use npm-link...
From the docs:
Go to the node_modules directory, and, inside each package, run npm-link:
$ cd node_modules
$ cd package-name
$ npm link
$ cd ..
...
In the directory of your project which needs the local modules:
$ npm link package-name

Modules are not installing globally with "npm -g install..."

On Ubuntu 12.04 x 64...
npm -g install hiredis redis
Installs fine and npm ls shows those modules, but only when I'm in node source directory
does not show when I'm in any other directory
For kicks, tried running the command while in that other directory- still no dice :(
They are installing globally, but you cannot see them with npm ls, in other directories. Because npm ls only shows local modules. If you want to list global modules you have to type: npm ls -g.
Sometimes another version or just a wrong path is referenced in the npm config file instead of the installed version.
This may cause node/npm to misplace global modules.
To check and fix:
In cmd line type: npm config list
You should get a list of configuration values, one of them is prefix.
Make sure the path in prefix is the same path (only without node.exe) as the actually installed node.exe path.
(this path is listed further down as node bin location)
If it's not, change it:
Either in the config file (in your user folder, named .npmrc)
Or, via cmd line: npm config set prefix "C:\Program Files\nodejs" (change path for ubuntu of course)
Reinstall the module/package you tried to install, don't forget -g for global.

Resources