18

I've been using node and npm forever. Right now, however, when do command shift p, Tasks: Run task, npm build, I get this error message:

Executing task: npm install <

/bin/bash: npm: command not found The terminal process terminated with exit code: 127

Terminal will be reused by tasks, press any key to close it.

In the terminal, node and npm work fine. I even tried running /bin/bash; node -v and that works fine.

AskYous
  • 4,332
  • 9
  • 46
  • 82

6 Answers6

42

Since this is the first result on Google, I want to share my solution despite this being a year old. The problem is that VS Code isn't initializing the NPM command, and many others when using VS Code Tasks. The solution to this is to force VS Code to do so.

This is a very simple thing to do. Simply open up settings.json and add this:

{
  "terminal.integrated.shellArgs.linux": [
    "-i"
  ]
}

I know that this thread is a year-old, but it's the first result I found on Google when going on my search, and while AskYous's solution worked for him, it didn't for me.

see comments for changes due to new versions of VSC (1.60.1+?)

VirxEC
  • 998
  • 10
  • 22
  • 3
    This works for me after setting up a new Linux Mint computer. I'd love to know why I needed it on this one, but not on my last computer which had the same OS. I copied my VSCode settings over, so everything should be the same. – Jack Steam Jun 09 '20 at 14:25
  • 4
    I ran into this on macOS and used the above to fix it except I used terminal.integrated.shellArgs.osx instead of linux and it worked after restarting VSCode. – Joshua Dyck Dec 23 '20 at 04:29
  • 1
    Excellent. Fixed the exact same issue for me as well :) – mspoulsen Jan 05 '21 at 22:40
  • Switched from linux to osx, worked out pretty good, thanks! – Fabio Martins Feb 07 '21 at 15:08
  • 3
    This worked for me on fresh Ubuntu (20.04). FYI, just to re-affirm, preferences related to this are in `Preferences/Features/Terminal`. – Kalnode Feb 27 '21 at 18:53
  • Using VSCode Version: 1.60.1 and `terminal.integrated.shellArgs` is not a setting anymore. – Jason Kuhrt Sep 17 '21 at 15:31
  • 1
    Using https://code.visualstudio.com/docs/editor/integrated-terminal#_terminal-profiles worked for me though, where args can be set. – Jason Kuhrt Sep 17 '21 at 15:38
  • 1
    Great! This has worked for me but curious to know why `-i` is needed – Premkumar chalmeti Oct 20 '21 at 12:38
  • 1
    Hey @VirxEC , can you update the snippet to this: https://stackoverflow.com/a/70816845/1664811 ? I tried posting in the comment, but comments won't show multi-line snippets, and the edit queue on your answer is full. – AlenL Jan 22 '22 at 20:25
18

For versions where there is warning for VirxEC solution, can use

    "terminal.integrated.profiles.osx": {
        "zsh": {
            "path": "zsh",
            "args": [
                "-i"
            ]
        }
    },
    "terminal.integrated.defaultProfile.osx": "zsh",

from https://code.visualstudio.com/docs/editor/integrated-terminal#_terminal-profiles

tonisives
  • 1,962
  • 1
  • 18
  • 17
  • Open Settings (cmd + ,). Search for Osx. Click where it says edit in setting.json. – Resonance Sep 23 '22 at 22:34
  • Setting the defaultProfile (`"terminal.integrated.defaultProfile.linux": "bash"` in my env) fixed all my issues. Good one thanks – Antonin Cezard Feb 13 '23 at 13:37
  • Holy cow, 5 hours and 20 threads later, this worked for me. – Kalnode Aug 19 '23 at 13:48
  • Thank You so much for sharing this! I checked few different solutions before and they didn't work for me, but this one really helped! What I've changed though is the "structure" and "path": `"terminal.integrated.automationProfile.linux": { "path": "bash", "args": [ "-i" ] }`. Glory and thanks to the One! Bless! – kcpr Aug 29 '23 at 10:21
12

Update to @VirxEC's answer for newer versions of VSCode (1.60+ ?) the correct setting is (change the actual shell path/name as desired):

  "terminal.integrated.profiles.linux": {
    "bash": {
      "path": "bash",
      "icon": "terminal-bash",
      "args": ["-i"]
    }
  },
AlenL
  • 418
  • 3
  • 7
  • Doesn't work for me, still getting npm command not found. Why is this needed anyway, is there a ticket for this? Seems kind of wild this isn't working out of the box. – E. T. Nov 06 '22 at 22:14
3

I found the solution. I had to tell nvm the default node version because I uninstalled a version before I had this error. See here: https://stackoverflow.com/a/34777308/1404347

AskYous
  • 4,332
  • 9
  • 46
  • 82
0

I had the same issue, though I was using zsh and nvm, but if you also happen to get this error at startup:

"Unable to resolve your shell environment in a reasonable time. Please review your shell configuration."

Then maybe this fix would work for you too.

Sameh
  • 666
  • 5
  • 9
0

This can also be caused by installing the unofficial vscode snap package. Since the app is contained it does not have access to npm.

For reference:

https://github.com/flathub/com.visualstudio.code#readme

It's especially easy to install the snap package by mistake on Ubuntu. To fix the issue: uninstall the snap version and install the "non snap" official version.

fast-stack
  • 23
  • 3