Run bower from root user, it's possible? How? - node.js

I have a local development server where I test a lot of things, now I'm playing with bower to manage the libraries' dependencies in my Symfony2 project. After getting NodeJS (v0.10.31) installed and bower (1.3.9), I tried to run the command sp:bower:install which belongs to Symfony2 SpBowerBundle from console as root:
Symfony > sp:bower:install
Installing bower dependencies for "TemplateBundle" into "/var/www/html/tanane/src/Tanane/TemplateBundle/Resources/config/bower/../../public/components"
bower ESUDO Cannot be run with sudo
Additional error details:
Since bower is a user command, there is no need to execute it with superuser permissions.
If you're having permission errors when using bower without sudo, please spend a few minutes learning more about how your system should work and make any necessary repairs.
http://www.joyent.com/blog/installing-node-and-npm
https://gist.github.com/isaacs/579814
You can however run a command with sudo using --allow-root option
I know that adding --allow-root works since I tested directly from bash but it apparently isn't allowed from the bundle command line. Now, is the only way to run bower as root to add --allow-root or does it exist another way ?

below answer is for symfony framework's bundle,
but if you come here from google using phrase "bower root" you have two options to solve that:
add --allow-root to command
set global bower config that will allow
running bower as root
Option 1: you can run bower as root by typing:
bower install --allow-root
root is allowed by setting --allow-root command parameter
Option 2: is using global setting that allows root, by creating file:
/root/.bowerrc
which have inside following configuration:
{ "allow_root": true }
how to do this in SpBowerBundle symfony bundle:
probably you haven't set sp_bower.allow_root to true in SpBowerBundle config
in bundle config, by default you have set something like this:
allow_root: false # optional
but you should have:
allow_root: true
so in app/config/config.yml add this bundle config
sp_bower:
allow_root: false # optional
bundle config reference (all settings):
https://github.com/Spea/SpBowerBundle/blob/master/Resources/doc/configuration_reference.md

If you are encountering this issue on Docker containers just add this line in your Dockerfile:
RUN echo '{ "allow_root": true }' > /root/.bowerrc

I fixed a similar problem by changing the directory permissions:
sudo chown -R $USER:$GROUP ~/.npm
sudo chown -R $USER:$GROUP ~/.config

This might be stupid but for me bower install --allow-root did not work but bower --allow-root install did, using grunt-bower-install version 1.6.0
This was on a docker running with root user, perhaps will save someone some time :)

Faced similar issue when installing swagger-editor.
Changed the following line in package.json from
"bower-install": "bower install"
to
"bower-install": "bower install --allow-root"

This works for me (add -u parameter on docker run)
bash
docker run -it -v ${PWD}:/www -w /www -u node node ./node_modules/bower/bin/bower install

For My case it is in Pom.xml where i have added as an argument as below:
<executable>bower</executable>
<arguments>
<argument>install</argument>
<argument>--allow-root</argument>
</arguments>
If need to avoid this --allow--root parameter we can do a compile out of root user

remover / delete bower_components
and reinstall bower
bower install

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

npm command 'serve ' not found, although it is installed

I have installed serve with npm as "npm install serve -g" and also with yarn "yarn global add serve", but when I try to run "serve -s build" it says that "Command 'serve' not found.
You should not install the packages globally.Try to do the following-
npm uninstall -g serve
npm i -S serve
Let me know if this works.
I had same problem too and this helped me to fix it so try this after installing serve;
npx serve -s build
or
npx serve -s build -p 8000
(8000 = it depends by your choice)
I don't know why but this worked for me
None of these above answers worked for me, so this is what works for me :
sudo su
npm install -g serve
Installing as root helps globally installing serve
Make sure to have this in your .bashrc or .zshrc
if you're using Yarn:
export PATH="$PATH:$(yarn global bin)"
if you're using NPM:
export PATH="$(npm bin -g):$PATH"
So that the shell would know where to look for executables such as serve, npx, live-server etc that are installed globally.
Make sure to reload your shell config:
source ~/.bashrc // or ~/.zshrc
If anyone still gets the problem, try this:
npm uninstall -g serve
npm i -S serve
yarn global add serve
I faced the same problem, what I did was run the command yarn serve -s build
If you got it installed with npm then you can just add npm before the suggested command

Running 'bower install' on mac

I am trying to run bower install after the installation of bower using npm is done. But the problem is when I run sudo bower install, it says bower should be run without sudo. If one runs bower install it says that permission denied cannot access file - ~/.config/configstore/bower-github.json
I tried switching to root user but no luck. What can be done to fix this?
This is how it should be solved apparently. Run the following
sudo chown -R *username* ~/
That should solve it!

Customize AWS ElasticBeanstalk NodeJS Install (use yarn)

Isit possible to configure EBS to install my NodeJS application using yarn package manager instead of NPM?
I've figured out a way, but it is a little hacky.
Create a .ebextensions/yarn.config file. (The name does not have to be 'yarn'.)
Put this content into the file:
files:
# Runs right before `npm install` in '.../50npm.sh'
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/bin/bash
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
# install node
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -;
# install yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo;
yum -y install yarn;
# install node_modules with yarn
cd "${app}";
yarn --production;
This ebextension creates a file which does 3 things:
Installs node.
Installs yarn.
Installs node_modules with yarn.
In order to make Elastic Beanstalk run yarn install before it runs npm install, the file is created under /opt/elasticbeanstalk/hooks/appdeploy/pre. This turns the file into a pre-deployment hook, which means that Elastic Beanstalk will run it during the first phase of deployment. By default, there is another file in this directory called 50npm.sh, which runs npm install. Since Elastic Beanstalk runs the files in this directory alphabetically, 49yarn.sh (our file) will run before 50npm.sh (the default file), resulting in yarn install running before npm install.
One potential problem is that the environment variables set in the Elastic Beanstalk UI (under Configuration > Software Configuration) are not available at this point of the deployment phase. This is a big problem if you have an npm auth token there which you use to install private npm modules.
Another potential problem is that this installs node manually, so the "Node version" you specify in the Elastic Beanstalk UI (under Configuration > Software Configuration) will have no effect on the version of node your application uses; you need to specify it in this ebextension. Elastic Beanstalk's 50npm.sh both installs node and runs npm install. Since we have to run yarn install before that file runs, we also have to install node manually. Then, when Elastic Beanstalk goes to install node, it detects that node is already installed but does not verify that it is the correct version, so it skips the node installation.
For reference, the yarn installation instructions came from here: https://yarnpkg.com/docs/install#linux-tab
I did this following instructions on https://yarnpkg.com/lang/en/docs/install/
commands:
01_install_yarn:
command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"
This way that i came up with lets you still control the node version via the Elastic Beanstalks Dashboard.
Thanks for this question! couldn't have come to this solution without it :D
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script
# (https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663).
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
# But their default script actually doesn't work at all, since the app
# staging dir, where they try to run `npm install`, doesn't exist during
# config deploys, so ebnode.py just aborts:
# https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L140
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
tmp="$(mktemp || bail)";
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
version="$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)";
echo $version
major="$(cut -d'.' -f1 <<<${version})"
yum -y install python26 python26-libs
wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo;
wget "https://rpm.nodesource.com/pub_${major}.x/el/7/x86_64/nodejs-${version}-1nodesource.x86_64.rpm" -O "${tmp}";
rpm -i --nosignature --force "${tmp}";
rm -f "${tmp}";
yum -y install yarn;
cd "${app}";
yarn --production;
Had to revisit this as we couldn't figure out why we were stuck on node 8 even though we set it to node 12 in the EB UI. Seems that if you install a global node it overrides the version setting. Instead of installing a global node, this uses the Elastic Beanstalk node install and adds it to the path. You have to add the PATH in again at the start of your yarn install script but it seems to be the least invasive way to use yarn.
content: |
#!/usr/bin/env bash
set -euxo pipefail
EB_NODE_VERSION=$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)
echo "EB node version: $(EB_NODE_VERSION)"
# Make sure Node binaries can be found (required to run npm).
# And this lets us invoke npm more simply too.
export PATH=/opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-x64/bin:$PATH
if yarn -v; then
echo 'Yarn already installed.'
else
echo 'Installing yarn...'
npm install yarn -g
fi
An easy way to prevent EB from running npm install is to create an empty node_modules folder in a prebuild hook:
Edit your_project/.platform/hooks/prebuild/prevent-npm.sh:
#!/bin/bash
# EB build scripts will not install using npm if node_modules folder exists
mkdir node_modules
This way, EB will still install Node.js, so you don't have to install that yourself. But it will skip npm install when node_modules exists.
Then inside the predeploy hook, you can run yarn. If you are running Node.js 16 or newer, you can use corepack yarn, and it will just work (you don't have to install yarn from yum, npm or anything else.)
Edit your_project/.platform/hooks/predeploy/yarn.sh:
#!/bin/bash
corepack yarn
Be sure to also create symlinks under confighooks in your project, or else it will fail to build when changing config (e.g. updating environment values):
mkdir -p .platform/confighooks/{prebuild,predeploy}
ln -s ../../hooks/predeploy/yarn.sh .platform/confighooks/predeploy/yarn.sh
ln -s ../../hooks/prebuild/prevent-npm.sh .platform/confighooks/prebuild/prevent-npm.sh
Since get-config is no longer present in the new Amazon Linux 2 platform, we had to figure another clean way to do this, and came up with the following :
container_commands:
01_npm_install_yarn:
command: "npm install -g yarn"
10_yarn_install:
command: 'PATH="$PATH:$(dirname $(readlink $(which node)))" yarn install'
You may want to put the PATH= logic in a script and call it before every yarn command, to have clean command: instructions in your extentions.
Also, note that if you install yarn using the yum package manager, you completely break the NodeJS version management provided by Beanstalk (since it the black magic running behind make some symlinks in /bin and /usr/bin).

Unable to create yeoman webapp

I get this when I try to create a webapp with yeoman.
/usr/local/lib/node_modules/yo/node_modules/insight/node_modules/configstore/configstore.js:66
throw err;
^
Error: EACCES, permission denied '/root/.config/configstore/insight-yo.yml'
You don't have access to this file.
at Object.fs.openSync (fs.js:436:18)
at Object.fs.writeFileSync (fs.js:975:15)
at Object.create.all.set (/usr/local/lib/node_modules/yo/node_modules/insight/node_modules/configstore/configstore.js:56:8)
at Object.Configstore (/usr/local/lib/node_modules/yo/node_modules/insight/node_modules/configstore/configstore.js:19:11)
at new Insight (/usr/local/lib/node_modules/yo/node_modules/insight/lib/insight.js:23:34)
at process.<anonymous> (/usr/local/lib/node_modules/yo/node_modules/insight/lib/push.js:11:16)
at process.emit (events.js:98:17)
at handleMessage (child_process.js:322:10)
at Pipe.channel.onread (child_process.js:349:11)
I googled and I tried the following things, but it didn't help:
chown root /root/.config/configstore/insight-yo.yml
chown myusername /root/.config/configstore/insight-yo.yml
Installed npm-sudo-fix and ran it, and no luck yet.
I am on Debian Wheezy and have npm 1.4.4.
I was facing the same issue. I ran the command below, It fixed my problem:
mkdir -p /root/.config/configstore
chmod g+rwx /root /root/.config /root/.config/configstore
The same issue happened to me, i was in root mode user#. I just get back to normal user$ and it worked. i am using Zorin OS
I was able to fix this by running:
echo prefix = ~/.node >> ~/.npmrc
and
export PATH=$HOME/.node/bin:$PATH
Explanation here:
Error installing yeoman
What does `echo prefix = ~/.node >> ~/.npmrc` mean?
npm / yeoman install generator-angular without sudo
I just fixed it in Windows. I have done three things, which one have fixed it I am not sure.
Run cmd prompt as administrator.
takeown /? this command is equivalent to chown (linux) but for windows.
cacls -cacls /E /T /G :F
simply go in $USER /Users/calvin/.config/configstore/ and make files unhidden.
it worked for me like charm. I have been trying to fix this since many days.
I had the same issue building a meanjs app on Ubuntu and I've fixed this issue using sudo instead of root:
sudo npm install -g yo generator-meanjs bower grunt
Then i could build the app without being asked for password again:
yo meanjs
sudo allows user to act as root without root login, it is more secure to use sudo instead of logging in as root.
sudo vs root - askubuntu
Although the chmod command seemed to work at first, I got a similar error later, for a different folder. But here I found this workaround which fixed that too:
sed -i -e '/rootCheck/d' "${NPM_CONFIG_PREFIX}/lib/node_modules/yo/lib/cli.js"
If yo is run as root, rootCheck will downgrade to UID 1000 (on Linux) or 501 (on OS X). This causes the permission error. But in a docker container, you might want to run as root, so you have no other option.
You need to rerun this command after upgrading yeoman. In case NPM_CONFIG_PREFIX is not defined, you can use:
export NPM_CONFIG_PREFIX=$(npm config get prefix)
the issue is that yo is not granted to use some of the nodejs modules. basically, y the problem is related to authorization of yo by node package manager "npm", to fix this run this command first
sudo npm install --global yo --allow-root

Resources