How to install node/npm so that 'sudo npm' works on an ec2 instance? - node.js

Answer: Follow the steps in this link
http://iconof.com/blog/how-to-install-setup-node-js-on-amazon-aws-ec2-complete-guide/
I am running ubuntu on an ec2 instance and need to sudo npm install something. I am given the error that sudo npm is not recognized.
There is a previous thread that is 7 years old. On EC2: sudo node command not found, but node without sudo is ok I tried every solution there and nothing worked.
I think the problem is using NVM to install npm and node. How can I install these so that they are compatible with sudo? Thanks!

You can run the following command
sudo yum install nodejs npm --enablerepo=epel
For more, you can also install using the following link also: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html
Second Approach:
Install NPM
git clone https://github.com/isaacs/npm.git
cd npm
sudo env PATH=$HOME/local/node/bin:$PATH make install
Now you can install npm packages and run javascript node files as usual!
sudo npm install forever -g
sudo npm install express -g
Resource Link: http://www.wisdomofjim.com/blog/how-to-install-node-and-npm-on-an-ec2-server

You can use NVM. You can follow this link which gives proper steps to install node on ec2 instance.

I dont know if my problem is the same as yours (i think is similar), but i tried to run "sudo npm install" on a amazon linux ec2 machine and it gave me the "sudo: npm: command not found" type of problem.
I followed the instructions recommended by AWS to install node and NPM (https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html).
I solve this by changing the owner and group of the directory and files (package.json and package-lock.json):
chown ec2-user:ec2-user nameDir
and then i run npm install again. It worked!

Related

Create vue Permission denied

I'm trying to install vue app using this command "npm init vue#latest",
But the following error occurred with me, any help :(
Note : I tried many solutions like : chmod -R 755 .npm and sudo chown -R mostafa .npm but nothing changes
maybe you could try using the vue-cli like this:
npm install -g #vue/cli && vue create hello-world
If this wouldn't work then I'd suggest to reinstall node/npm
You should probably also make a clean installation of these tools so that you don't need to run such things as administrator
In order to install vue on your machine you must first have installed nodejs ( package manager node package manager ) to which the acronym npm refers, I leave you the linux commands under :
install :
sudo apt install nodejs
shows the installed version of nodejs :
node -v
After installing nodejs try relaunching the command to install vue
if you have already installed them try with this :
add write permissions to the study folder , and not to the mostafa folder , linux only assigns permissions to the folder in the chmod command and not subfolders, try these commands :
cd home/mostafa/Dowloads
sudo chmod ugo+w study
Are you not being blocked by SELinux directives?
Try running the same command using your least privileged user (normal user).
And please avoid using the root user for everyday tasks.
You need to have installed Nodejs, vue and create-vue.
sudo apt install nodejs
npm install vue#latest
npm install create-vue#latest
Now you can run the command to start the new project with Vue.
npm init vue#latest

I cannot install nodemon by using npm command

enter image description here
When i tried to install nodemon package using npm i got errors as shown in the picture. I also tried using sudo but nothing changed.
you can use this command.
sudo npm install nodemon -g
sudo makes sure that the command would run as a superuser that allows users to run programs with the security privileges of another user (normally the superuser, or root).
-g instructs npm to install the node package globally onto the system.

getting error when using -g in npm

when I try to install any package globally, I get multiple errors. have a look at the image
the errors in the terminal
Thanks all.
When you install any global package just add sudo prefix. sudo npm install -g express
If you want to install packages globally with the "g"-parameter you must be root.
It's easy to just run sudo npm install -g express to successfully install express.
But a long time solution for the permission access, run chown -R YOUR_USERNAME /usr/lib/node_modules from your terminal so that you will not have to include sudo in your subsequent installation using -g

Fix for npm global install on Ubuntu

I have a nodejs package that requires a global install. This one fails in a way that leads me to believe there might be a configuration problem in the the Ubuntu package npm. This happens every-time I setup an Ubuntu 14.04 machine.
sudo apt-get install npm
npm install -g lineman
The npm -g command will throw some access error naming the local lib and bin directories. Unlike some global installs, it is not an option to cheat and run the second command under sudo. So, the only fix I have found that will work is something like this:
sudo chgrp -R $(whoami) /usr/local/bin /usr/local/lib
sudo chmod -R g+rwx /usr/local/bin /usr/local/lib
The fix is fine for me, I'm the only user. But is this really the best way to do it? I don't want to document my fix for anyone else that might use it in an environment where this will not work or cause trouble.
Also, should I file a bug report with someone who packages npm for Ubuntu?
Instead of npm install -g lineman, you should run sudo npm install -g lineman. npm requires permission as well.
Also check this stackoverlfow link.

How do I know whether I have Node.js and npm successfully installed on Ubuntu 14.04?

I installed Node.js with these instructions and it seemed successful:
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Then I installed npm with these instructions:
sudo curl https://www.npmjs.org/install.sh | sh
The nodejs installation seemed to work without errors but the npm command gave me a lot of errors. But it seems like they are installed because when I test what version I have they both come up:
nodejs -v
v0.10.30
npm -v
1.4.21
So If this doesn't tell me that I have both programs successfully installed, which I assume I do not, how do I know?
I think your tests tell that both or properly installed.
But you can try just type node in terminal & it should open a node shell, where you can check by running basic commands.
Current distributions of node.js (including the one you downloaded) already include npm. So maybe installing npm manually is one source of your errors. Beware that usually you run "npm install" with the permissions of a regular user. There are only some npm-based utilities that are to be installed with root permissions and the '-g' (global) command line switch.
On linux if you wish to install node.js and npm as yourself NOT root :
to start fresh remove prior node.js and npm installs as well as these :
~/.npmrc
~/.npm
~/tmp
~/.npm-init.js
create your ~/bin/ directory if not already created :
mkdir ${HOME}/bin
download source from : http://nodejs.org/download/
cd node-v0.10.30/
./configure --prefix=${HOME}/bin/nodejs
make -j8
make install
which puts it into dir defined by above --prefix
export PATH=${HOME}/bin/nodejs/bin:$PATH
define NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into dir in curr dir :
export NODE_PATH=${HOME}/bin/nodejs/lib/node_modules
do above AND use syntax :
npm install xxxxx -g
always use the -g for global which puts package xxxxx into $NODE_PATH
NOTE - nodejs install gives you npm as well :
ls -la ${HOME}/bin/nodejs/bin

Resources