Installed truffle globally using npm but still cannot access it - node.js

I have installed nodejs and then tried running the following command to install truffle suit and ganache as follows:
npm install -g truffle
npm install -g ganache
It got installed as follows:
Installing truffle and ganache
I also checked whether its present in the directories or not by running the command:
npm list -g
Yet when I run the command:
truffle -v
ganache -v
I get the error:
Error after installation
The package although it was installed globally, I couldn't access the package. I do have VS CODE and python 3.11 installed. Any suggestions to how can I use it globally?
I did try the following:
running npm config edit
changing the prefix to 'C:\Users\username\AppData\Roaming\npm'
adding that path to the system path variable
reinstalling the package with -g.
explicitly set the path by
npm config set prefix c:/Users//AppData/Roaming/npm
Change the permission of node global package without tampering with default file permission by
rather changing the directory itself along creating a new profile.
mkdir ~/.npm-global
npm config set prefix ~/.npm-global
set PATH=~/.npm-global/bin:$PATH
source ~/.profile
npm install -g <npm_packages>
But none of them worked

Related

Package visible in npm list, but cannot be required

I am using nvm and via it I have set node v8.11.3 as my default. I installed the varint package globally using npm install -g --verbose varint, specifically to see where it is being installed. Post install on running npm list -g I can see it, but requiring it in the node repl fails. I am not sure as to what the issue is here. I am also not in a directory which has a package.json from before.
UPDATE:
which npm : ~/.nvm/versions/node/v8.11.3/bin/npm
which node : ~/.nvm/versions/node/v8.11.3/bin/node

How to uninstall global package with npm?

I have installed webpack in this way:
npm install -g webpack
Now want to uninstall it:
npm uninstall -g webpack
Check it again, it didn't been uninstalled:
webpack -v
3.1.0
Why?
And, I use this way can't find webpack:
npm list -g | grep webpack
This also didn't work:
npm uninstall -g webpack --save
After run this under a directory which included package.json:
npm uninstall webpack
npm WARN babel-loader#6.4.1 requires a peer of webpack#1 || 2 || ^2.1.0-beta || ^2.2.0-rc but none was installed.
npm WARN uglifyjs-webpack-plugin#0.3.1 requires a peer of uglify-js#^2.8.0 but none was installed.
npm WARN uglifyjs-webpack-plugin#0.3.1 requires a peer of webpack#^1.9 || ^2 || ^2.1.0-beta || ^2.2.0-rc but none was installed.
Try running both of the below commands:
npm uninstall -g webpack
npm uninstall webpack
I think you might be checking/looking at the local version after deleting only the global one.
You have to remove the packages manually installed globally on your os with sudo:
On OsX navigate to this directory
cd /usr/local/lib/node_modules
and
sudo rm -rf <packageName> // sudo rm -rf webpack
npm uninstall -g webpack
Worked for me, try running the command prompt in administrator mode.
You're most likely running a file from another install of npm.
Run which webpack to see where your shell is finding webpack.
Run npm root -g to find the root of the tree it's supposed to be in, being sure you're running the correct npm with npm -v and which npm.
If your webpack bin isn't in the npm root, reset your path to the webpack binary e.g. hash -d webpack in bash, and then go remove the unwanted npm root from your PATH variable. You can now use npm install -g webpack and npm uninstall -g webpack and it should work.
I have tried uninstalling global packages in several ways.
npm uninstall -g <package_name> this didn't work.
I managed to remove the global packages in the following way:
Goto terminal
Run this command npm list -g
Goto the path (C:\Users\user\AppData\Roaming\npm)
Delete all the related files to your package
Goto node_modules find and delete the package
This should work.
YW!
If you are using Node Version Manager (nvm) and you want to remove a global system package you will need to switch to that version. For example:
nvm use system
npm uninstall -g webpack
Try
chown -R "$(whoami)": "$(npm root -g)"
(you may need sudo for it) and then npm uninstall -g again
on windows run as administrator and run the command
npm uninstall -g webpack
on Linux
sudo npm uninstall -g webpack
In Windows, open the cmd with Administrator rights (start -> type cmd -> right-click on icon -> open with adm. rights), then navigate (in cmd type "cd ../../users/your_user_name") to your user folder, then run
npm uninstall -g webpack
or (I don't remember which one worked for me)
npm uninstall webpack
Had the same issue an none of the answer above helped.
My project was in a sub-directory of a larger project, which also had a node_modules folder.
That's why it says, something like "found another version higher in the tree."
Delete that folder, go back to your sub-dir, remove node_modules and package-lock.json, and finally run npm install again.
In archlinux, after running
npm uninstall -g <package_name>
you might have to manually enter /usr/lib/node_modules/ to remove the package's directory. This will prevent conflicts if you try reinstalling that package with a different package manager like pacman.
Building on #karthik006 's answer of deleting directly from the global node_module folder, if you are using nvm, you first need to decide from which version of node you want to delete the global package.
After that, switch to that version of node using nvm use <version>
Then run nvm which current to get the path where the executable for this version of node is saved.
The path will be something like <path to nvm node dir>/<node version>/bin/node
Using this path, cd into <path to nvm node dir>/<node version>/lib/node_modules and then rm -rf the packages that you want to remove.
On ubuntu the package I was on the hunt for was buried in ~/.npm/_npx/<chars>/node_modules/ and in ~/.npm/_npx/<chars>/node_modules/.bin/. I removed the bin and the directory and got the reinstall prompt.
Try This:
npm uninstall -g <package_name>
E.g: npm uninstall -g webpack

"npm config set global false" not working

For some reason my npm is configured to install packages globally. I may have selected that option when I initially installed npm. However, I'm trying to change that configuration so npm installs locally. I googled and found this option:
npm config set global false
However, npm is still installing globally instead of locally. Am I missing a step or doing something incorrectly? Would you expect the command above to do what I need it to do?
npm installs packages locally by default. If you want to install a package globally use flag -g.
e.g. npm install my-package -g

How does npm know the path to install module from

I am new to npm, using package.json to define the dependencies. I am just curious where is those dependencies installed from?
First, npm checks your local machine for packages installed globally, or using the -g flag.
ie: if you used npm install -g moduleName
You can run npm list -g to figure out where that is on your machine.
Otherwise, npm goes to where you set the npm registry. By default this is npmjs.
If you want to see the default settings, run npm config list

NODE_PATH no effect

I would like that
~ $ npm install express"
will not install express in my home folder, but instead in ~/apps/node_modules.
So I inserted this export NODE_PATH=/home/a/apps/node_modules in .bashrc and than I did source .bashrc.
Unfortunately, npm install express still installs it in /home/a/node_modules and npm install -g express require root/Administrator.
How is it possible that npm install packages in /home/a/apps/node_modules?
NODE_PATH is used to find modules, but not to install them.
You can use --prefix to signify where npm should install a package:
npm --prefix ~/apps install express
You could make it more permanent by configuring npm to always use that prefix, but that would only overwrite the global location. So you would need to use the -g flag:
# config once
npm config set prefix ~/apps
# after that, '-g' will install packages in ~/apps
npm install express -g
Also, using -g will install packages in ~/apps/lib/node_modules (which, I think, cannot be changed), so add that directory to your $NODE_PATH as well.
Another solution would be to create a shell alias for npm:
# in your ~/.bashrc
alias npm="command npm --prefix ~/apps"
Although I'm not entirely sure how that would work together with -g.

Resources