Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Npm fails #2535

Closed
ahmadmayahi opened this issue May 2, 2021 · 8 comments
Closed

Npm fails #2535

ahmadmayahi opened this issue May 2, 2021 · 8 comments

Comments

@ahmadmayahi
Copy link
Contributor

ahmadmayahi commented May 2, 2021

  • Deployer version: 6.8.0
  • Deployment OS: macOS BigSur 11.3

The following task fails without giving a reasonable reason:

task('build:npm', function () {
    run('cd {{release_path}} && npm install && npm run prod');
});

Error message:

  The command "cd ~/mysite/releases/4 && npm install && npm run prod" failed.

  Exit Code: 127 (Command not found)

  Host Name: xx.xx.xx.xx

  ================
  bash: line 1: npm: command not found

Both npm and node do exist in the following paths:

which node
/home/web/.nvm/versions/node/v16.0.0/bin/node

which npm
/home/web/.nvm/versions/node/v16.0.0/bin/npm
@antonmedv
Copy link
Member

Verify what npm in path for non-login shell.

Also please check with dep v7.

@klaasnicolaas
Copy link

klaasnicolaas commented Nov 10, 2021

Did you ever found a solution? I run into the same problem in the 7.x versions.

[prod]  error  in npm.php on line 25:
[prod] run command -v 'npm' || which 'npm' || type -p 'npm'
[prod] exit code 1 (General error)

@Schrank
Copy link
Contributor

Schrank commented Nov 10, 2021

Easiest fix: use absolute paths. If this works, the problem is your $PATH variable.

@jawira
Copy link

jawira commented Dec 1, 2021

Same here. I even used absolute paths, but then I got this error:

In Client.php line 103:

[Deployer\Exception\RuntimeException (127)]
The command "cd /var/www/myproject/releases/5 && (/home/myuser/.nvm/versions/node/v12.22.7/bin/npm install --production)" failed.

Exit Code: 127 (Command not found)

Host Name: xxxxxxxx

================
/usr/bin/env: ‘node’: No such file or directory

So npm can't found node.

I used nvm to install npm.

@jawira
Copy link

jawira commented Dec 1, 2021

In my case the problem was nvm:

  1. I uninstalled nvm (including node & npm) as described in How to uninstall nvm? nvm-sh/nvm#298
  2. Use PPA to install node and npm as described in https://github.com/nodesource/distributions#installation-instructions
  3. Done, deployer works like a charm :)

@matiasvelizb
Copy link

If you still wants to use NVM in the older versions without much configuration, you need to source it everytime you run a command, since deployer is working with a non interactive shell.

I did this small workaround to avoid uninstalling nvm.

// NVM source, must be called every run command
set('nvm', 'source $HOME/.nvm/nvm.sh &&');

task('test', function () {
    $test = run("{{nvm}} npm --version");
    writeln($test);
});

@Mickeel
Copy link

Mickeel commented Jan 5, 2024

If you still wants to use NVM in the older versions without much configuration, you need to source it everytime you run a command, since deployer is working with a non interactive shell.

I did this small workaround to avoid uninstalling nvm.

// NVM source, must be called every run command
set('nvm', 'source $HOME/.nvm/nvm.sh &&');

task('test', function () {
    $test = run("{{nvm}} npm --version");
    writeln($test);
});

Thank you @matiasvelizb !
For anyone finding this still a problem, make sure to run all npm related command in the same 'run' command. Because whenever each 'run' command is separate shell session.

It still works in v7. Attached is our implementation.

// NVM source location
set('nvm', 'source $HOME/.nvm/nvm.sh');

// Must be called in every run command related to npm
// Note: do not separate run command, as it is accounted as different shell session
set('use_nvm', function () {
    return '{{nvm}} && node --version && nvm use 18';
});

// Tasks
desc('Installs npm packages');
task('npm:install', function () {
    run('{{use_nvm}} && cd {{release_path}} && npm ci');
});

desc('Build production assets');
task('npm:prod', function () {
    run('{{use_nvm}} && cd {{release_path}} && npm run prod');
});

@olivier1208
Copy link

If you still wants to use NVM in the older versions without much configuration, you need to source it everytime you run a command, since deployer is working with a non interactive shell.
I did this small workaround to avoid uninstalling nvm.

// NVM source, must be called every run command
set('nvm', 'source $HOME/.nvm/nvm.sh &&');

task('test', function () {
    $test = run("{{nvm}} npm --version");
    writeln($test);
});

Thank you @matiasvelizb ! For anyone finding this still a problem, make sure to run all npm related command in the same 'run' command. Because whenever each 'run' command is separate shell session.

It still works in v7. Attached is our implementation.

// NVM source location
set('nvm', 'source $HOME/.nvm/nvm.sh');

// Must be called in every run command related to npm
// Note: do not separate run command, as it is accounted as different shell session
set('use_nvm', function () {
    return '{{nvm}} && node --version && nvm use 18';
});

// Tasks
desc('Installs npm packages');
task('npm:install', function () {
    run('{{use_nvm}} && cd {{release_path}} && npm ci');
});

desc('Build production assets');
task('npm:prod', function () {
    run('{{use_nvm}} && cd {{release_path}} && npm run prod');
});

🎉 Awesome !

Here's the YAML version for whom it can help 😏

config:
  nvm: 'source $HOME/.nvm/nvm.sh'
  use_nvm: '{{nvm}} && node --version && nvm use 18'

tasks:
  npm:install:
    script:
      - '{{use_nvm}} && cd {{release_path}} && npm ci'

  npm:prod:
    script:
      - '{{use_nvm}} && cd {{release_path}} && npm run prod'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants