When running `bin/behat` I get a "Class not found" error - behat

When I run bin/behat I get this error.
PHP Fatal error: Class 'Symfony\Component\Console\Application' not found in
/vendor/behat/behat/src/Behat/Behat/Console/BehatApplication.php on line 31
My composer.json file contains this:
{
"require": {
"drupal/drupal-extension": "",
"behat/behat": "2.4.#stable",
"behat/mink": "1.4#stable",
"behat/mink-goutte-driver": "",
"behat/mink-selenium-driver": "",
"behat/mink-selenium2-driver": "",
"behat/mink-sahi-driver": "",
"behat/mink-zombie-driver": "",
"behat/mink-extension": ""
},
"minimum-stability": "dev",
"config": {
"bin-dir": "bin/"
}
}
And for some reason symfony/CssSelector is failing to clone:
[RuntimeException]
Failed to clone via git, https and http protocols, aborting.
git://github.com/symfony/CssSelector.git
fatal: No such remote 'composer'
https://github.com/symfony/CssSelector.git
fatal: No such remote 'composer'
http://github.com/symfony/CssSelector.git
fatal: No such remote 'composer'

I suggest removing bin, vendor folders and composer.lock file and running php composer.phar install again, the packages should install fine then.

Composer is a only a Method for installing Behat you can remove Behat and try with one of following Method :
The simplest way to install Behat is through Composer.
Method #1 (Composer)
Create composer.json file in the project root:
{
"require": {
"behat/behat": "2.4.*#stable"
},
"minimum-stability": "dev",
"config": {
"bin-dir": "bin/"
}
}
Then download composer.phar and run install command:
$ curl http://getcomposer.org/installer | php
$ php composer.phar install
Composer uses GitHub zipball service by default and this service is known for outages from time to time. If you get
The ... file could not be downloaded (HTTP/1.1 502 Bad Gateway)
during installation, just use --prefer-source option:
$ php composer.phar install --prefer-source
After that, you will be able to run Behat with:
$ bin/behat
Method #2 (PHAR)
Also, you can use behat phar package:
$ wget https://github.com/downloads/Behat/Behat/behat.phar
Now you can execute Behat by simply running phar archive through php:
$ php behat.phar
Method #3 (Git)
You can also clone the project with Git by running:
$ git clone git://github.com/Behat/Behat.git && cd Behat
$ git submodule update --init
Then download composer.phar and run install command:
$ wget -nc http://getcomposer.org/composer.phar
$ php composer.phar install
After that, you will be able to run Behat with:
$ bin/behat

try with loading --config option of behat.yml
bin/behat -v --config=app/config/behat.yml
Also not sure if you are running symfony or drupal instance.
See detail configuration of my behat+mink+selenium+symfony2.8 installation here

Related

how to use npm in ansible

This is my main.yml file in task:
- name: Use npm
shell: >
/bin/bash -c "source $HOME/.nvm/nvm.sh && nvm use 16.16.0"
become: yes
become_user: root
- name: Run build-dev
shell: |
cd /home/ec2-user/ofiii
npm install
npm run build-dev
become: yes
become_user: root
when: platform == "dev"
And the output when running the script:
fatal: [172.31.200.13]: FAILED! => {
"changed": true,
"cmd": "cd /home/ec2-user/ofiii\nnpm install\nnpm run build-stag\n",
"delta": "0:00:00.061363",
"end": "2022-11-09 09:45:17.917829",
"msg": "non-zero return code",
"rc": 127,
"start": "2022-11-09 09:45:17.856466",
"stderr": "/bin/sh: line 1: npm:命令找不到\n/bin/sh: line 2: npm:命令找不到",
"stderr_lines": ["/bin/sh: line 1: npm:命令找不到", "/bin/sh: line 2: npm:命令找不到"],
"stdout": "",
"stdout_lines": []
}
the error is "npm:command not found" but I am really sure about the installation and the path to be set appropriatelly in the machine, the thing that I doubting is the script
I don't know how to modify my script,I tried to use npm module, but I failed
The problem is that each task environment is separate and you are setting nvm environment in a separate task.
The "Run build-dev" knows nothing about the paths set up from "Use npm"
I'd suggest combining these two tasks, with a few additional changes explained below:
- name: Run build-dev
shell: |
source $HOME/.nvm/nvm.sh
nvm use 16.16.0
npm install
npm run build-dev
args:
executable: /bin/bash
chdir: /home/ec2-user/ofiii
become: yes
become_user: root
when: platform == "dev"
Additional changes:
Using bash -c "..." in a shell module would result /bin/sh -c "/bin/bash -c '...'", it's better to use executable: /bin/bash instead
Shell module has chdir argument to specify the directory the script to run at
Check shell module documentation for other arguments and examples.

ansible installing npm using nvm but returning npm command not found on npm install

I am trying to install npm with nvm using ansible playbook script on Ubuntu 18.04.2 LTS. It is getting installed but on running npm install command it returning an error ["/bin/bash: npm: command not found"]
this is the script
- name: Create destination dir if it does not exist
file:
mode: 0775
path: "/usr/local/nvm"
state: directory
when: "nvm_dir != ''"
- name: Install NVM
shell: "curl https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | NVM_SOURCE="" NVM_DIR=/usr/local/nvm PROFILE=/root/.bashrc bash"
args:
warn: false
register: nvm_result
This is the repository where I get the code (https://github.com/morgangraphics/ansible-role-nvm)
By default shell module uses /bin/sh unless the executable has been explicitly defined in the module using args/keyword.
Seems like /bin/bash(a variation of shell is not is installed on the host) thereby giving error. Script needs bin/bash.
bin/bash is mostly installed on all the operating systems. May be some path issue.
Also updated the code below with condition.
---
- hosts: localhost
tasks:
- name: Create destination dir if it does not exist
file:
mode: 0775
path: "/usr/local/nvm"
state: directory
when: "nvm_dir is not defined"
- name: Install NVM
shell: 'curl https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | NVM_SOURCE="" NVM_DIR=/usr/local/nvmPROFILE=/root/.bashrc bash'
args:
warn: false
register: nvm_result

Pass argument to npm script command by inserting it, not appending

I would like to insert arguments somewhere inside the command. Somehow to interpolate it. For instance:
{
"scripts": {
"foo": "git commit -am $message && git push"
}
}
Then run:
$ npm run foo -- --message "Baz"
Or something like this:
"foo": "git commit -am {0} && git push"
Then run:
$ npm run foo -- "Lorem"
Please note the git command here are just for demo purpose, we have different multiple commands. So Git Aliases are not the solution.
Is there is any way to achieve this behaviour with npm?
Thanks to #RobC, it turns out that we can specify the shell for npm scripts, so on windows I could just set the git bash for it and use #RobC solution here
$ npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
In package.json
"foo": "func() { echo \"$1\"; }; func"
Then run:
$ npm start foo "lorem ipsum"

How to enable nvm in steps in circleci 2.0?

Here are my steps in my
steps:
-run:
name: Setup nvm and npm
command: |
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
export NVM_DIR=$HOME/.nvm
source $NVM_DIR/nvm.sh
nvm install 8.9 && nvm alias default 8.9
-run: npm install && npm run lint && npm test
The second step always fails with this error message
/bin/bash: npm: command not found
I checked .bashrc and I can see the following lines are added to the end of the file
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Circleci 2.0 invokes the step command by starting a new shell with #!/bin/bash -eo pipefail
If I starts a docker (docker run -i -t buildpack-deps:xenial) and apply the first step, and then start a new shell via #!/bin/bash -eo pipefail, I can see npm is available on the path
I am using docker for this project
version: 2
jobs:
test_main:
docker:
- image: buildpack-deps:xenial
So why does it fail in circleci 2.0 environment? How can I ensure npm will be available to step 2 from step 1?
I have tried to add [ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc" to ~/.bash_profile (in case .bashrc is not executed due to the non-interactive/non-login shell)
To reproduce the issue you can run circleci build with this .circleci/config.yml file
version: 2
jobs:
build:
docker:
- image: buildpack-deps:xenial
steps:
- run:
name: Setup nvm and npm
command: |
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
# Activate nvm
export NVM_DIR=$HOME/.nvm
touch $HOME/.nvmrc
source $NVM_DIR/nvm.sh
# Use node 8.9
nvm install 8.9 && nvm alias default 8.9
echo 8.9 > $HOME/.nvmrc
# Enable nvm in following steps
echo '[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"' >> $HOME/.bash_profile
# To fix npm install : "node-pre-gyp: Permission denied"
npm config set user 0
npm config set unsafe-perm true
npm install -g npx webpack webpack-cli jest
node --version
npm --version
- run: npm install
You will see the following error message:
====>> npm install
#!/bin/bash -eo pipefail
npm install
/bin/bash: npm: command not found
Error: Exited with code 127
Step failed
Task failed
The problem lies with these lines:
# Enable nvm in following steps
echo '[ -s "$HOME/.bashrc" ] && \. "$HOME/.bashrc"' >> $HOME/.bash_profile
I was hoping to source .bashrc from .bash_profile. However since the shell of circleci is non-interactive, the environment variable PS1 is blank. Hence .bashrc basically quits immediately once it is sourced, because of this line in .bashrc
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
I have to put the following lines directly in the file specified by $BASH_ENV
echo 'export NVM_DIR=$HOME/.nvm' >> $BASH_ENV
echo 'source $NVM_DIR/nvm.sh' >> $BASH_ENV
I found that changing default node by nvm is not working for my steps.
Solved by:
- run:
name: 'Install Project Node'
command: |
set +x
source ~/.bashrc
nvm install 12
NODE_DIR=$(dirname $(which node))
echo "export PATH=$NODE_DIR:\$PATH" >> $BASH_ENV
Just source /opt/circleci/.nvm/nvm.sh in the beginning of every step.

How to run "nvm" in "oh my zsh"?

In the system there is a nodejs, installed through nvm. The command is not running npm.
Console is Oh my zsh
You can use zsh-nvm or enable it yourself by adding following lines to your ~/.zshrc
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Extra:
For faster shell initialization, I use lazynvm which only loads node when needed
lazynvm() {
unset -f nvm node npm
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
}
nvm() {
lazynvm
nvm $#
}
node() {
lazynvm
node $#
}
npm() {
lazynvm
npm $#
}
Reference: Lazy load nvm for faster shell start
Switching from Bash to Oh-My-Zsh
If you already have nvm installed and you're switching from bash to oh-my-zsh you can simply open up your .zshrc file and add the nvm plugin that is included with oh-my-zsh:
Open your zsh config file.zshrc in nano with this command: nano ~/.zshrc
Scroll down to where it shows plugins=(git) and add nvm inside the parentheses to make it show as plugins=(git nvm) (separate plugins with spaces)
Press control + O (on macOS), then enter, to save, then press control + X to exit
Then open a new terminal window/tab and enter nvm ls to confirm it works. Note that you must open a new window/tab for your shell to use the newly updated .zshrc config (or enter source ~/.zshrc, etc.)
Source: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/nvm
This worked for me on Ubuntu 20.04.
Install or update nvm
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Add in your ~/.zshrc
echo 'export NVM_DIR=~/.nvm' >> ~/.zshrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' >> ~/.zshrc
Load in the current shell environment
source ~/.zshrc
Check the nvm version
nvm -v
use homebrew to install nvm
brew install nvm
edit your system configuration
vim ~/.zshrc # or vim ~/.bashrc
export NVM_DIR=~/.nvm
esc > :wq
save file
reload the configuration
source $(brew --prefix nvm)/nvm.sh
view nvm version
$ nvm --version
# 0.36.0
enjoy it.
A much easier solution is to use the nvm plugin that is shipped by default:
It also automatically sources nvm, so you don't need to do it manually
in your .zshrc
git clone https://github.com/nvm-sh/nvm.git ~/.nvm
cd ~/.nvm && git checkout v0.35.1 (current latest release)
Add nvm to your ~/.zshrc. Ex: plugins=(... nvm)
I discovered that there is a nvm plug-in shipping with oh-my-zsh (that's different from lukechilds plugin). After short inspection, I think it adds the necessary modifications to .zshrc when loading, so simply adding nvm to the plugins list in .zshrc should work as well (and it does for me).
I did not find any more details on that default nvm plugin via google so I don't know whether this is the "go-to" solution.
Add this code to .zshrc on your user directory
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
Then run this code on your terminal:
source ~/.zshrc
With Linux (Ubuntu 20.04, 22.04 and 22.10)
With your favorite editor, you edit ~/.zshrc
nano or vi ~/.zshrc
At the end of the file, you add :
# NVM
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
And then you run :
source ~/.zshrc
I strongly suggest using christophemarois' approach to lazy loading nvm (node, npm and global packages) in order to avoid slow shell starting times:
# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")
# Lazy-loading nvm + npm on node globals call
load_nvm () {
export NVM_DIR=~/.nvm
[ -s "$(brew --prefix nvm)/nvm.sh" ] && . "$(brew --prefix nvm)/nvm.sh"
}
# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[#]}"; do
eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$# }"
done