18

I installed NPM using NVM.

When I use npm in Intellij terminal, it says I don't have NPM installed. But if I use Ubuntu terminal, it is working.

Here are what I tried:

I have already tried to set my node interpreter (in "Language and Framework" > "Node and NPM", set to ~/.nvm/versions/node/v6.8.0/bin/node).

I have also already enabled the Node.js Core library. There it shows the npm package is included.

But the IntelliJ terminal still complains I haven't installed npm. Why?

Raymond Pang
  • 465
  • 1
  • 5
  • 17

13 Answers13

10

It is because idea terminal launches a login shell by default, so the .bashrc file is not read.

To solve the problem:

Open "Settings" in IntelliJ. Then, expand "Tools" in the left panel, then click 'Terminal'.

Add -i to the Shell Path. (eg. /bin/bash -i)

Raymond Pang
  • 465
  • 1
  • 5
  • 17
10
ln -s "$(which node)" /usr/local/bin/node

The command will create a symlink from the current node version you have installed. If there is already a /usr/local/bin/node file, delete that since it might be a broken link.

Also change in Edit Configuration from whatever project version you have to "node" (which will be the link created with the above command) Edit Configurations

Tudor
  • 1,387
  • 1
  • 17
  • 15
9

This fixed it for me:

NVM patches environment variables on terminal startup only. If the IDE is launched from Terminal, it inherits Terminal environment (including modified PATH environment variable, added NVM_DIR env var, etc). In that case, there are normally no problems with using node/npm, because Idea sees the correct PATH value. For bash as shell, workaround could be the the following: edit your Idea launcher and set command to "/bin/bash -l -c "/path/to/idea.sh". This command will perform bash login (i.e. reading your .bashrc/.bash_profile files) and after that will run idea

https://intellij-support.jetbrains.com/hc/en-us/community/posts/205964744/comments/205060164

Just edit your Intellij launcher / startup script and change that to /bin/bash -i -c <path to idea.sh>

Dario Seidl
  • 3,557
  • 1
  • 33
  • 50
Tim
  • 833
  • 11
  • 11
6

I am running on Ubuntu and had the same issue shown by not being able to run node from the IntelliJ Idea terminal or being able to run gradle tasks that start a nvm related process.

Some of the existing answers fixed it for me when using the terminal within Idea, but running the gradle tasks still did not work. Starting it with bash -l -c... as found elsewhere did not work either.

Initially solved it by creating a start script that does the same as what nvm does, and then did the bash -l -c... and that worked, but found (after that) that the -i as stated above works as well and is simpler... So my .desktop exec entry line is now:

Exec=/bin/bash -i -c /opt/intellij/idea-IU/bin/idea.sh "%f"

Just mentioning it here as the accepted solution does not work for starting nvm related gradle tasks from within Idea.

Eric Aya
  • 69,000
  • 34
  • 174
  • 243
cfnz
  • 174
  • 2
  • 14
4

Adding the nvm node interpreter worked for me:

Settings -> Languages & Frameworks -> Node.js and NPM -> Node interpreter

Chose ~/.nvm/current/bin/node. If it doesn't show up in the interpreter selection, click ... and add it as a new path.

3

For me the solution was to move the following lines from ~/.bashrc to ~/.profile and reboot/relogin.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nidomiro
  • 792
  • 2
  • 8
  • 24
2

does it help if you run IntelliJ as admin? On Windows this solves many of the issues I had with setting up Node, Bower and Git

westworld.be
  • 199
  • 2
  • 7
  • Unfortunately, it is not working. I did gksudo -k -u root "/opt/idea-IU-162.2032.8/bin/idea.sh" %f to run IntellIjJ as root, and I tried node --version inside, and it still doesn't show anything. Thanks for your help anyway. – Raymond Pang Nov 03 '16 at 15:58
1

As Elena Pogorelova pointed out on the Jetbrains forums the reason why the IDE cannot find the Node script when using NVM is that:

When being launched from desktop/System menu, IDE only sees environment variables configured in ~/.profile (login shell), but not in interactive shell configuration files (like ~/.bashhrc).

So, what I found the best solution is moving the NVM initialization instructions from the ~/.bashrc file to the ~/.profile and reboot after that. In my case was:

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
pakobill
  • 316
  • 3
  • 8
0

The issue is that your environment(s) is(are) different from running intellij and which bash session you ran nvm <command (install/use)>. It is usually a good idea to add that line nvm recommends to add into your ~/.bashrc that will export the needed environment variables into each of your bash sessions. If you can run node --version from bash and it prints v6.8.0, your environment is correct, you just need to run intellij from that context, personally I'm not a fan of IDEs so I wouldn't know exactly, but I imagine intellij has a way to be executed from the command line.

Christian Grabowski
  • 2,554
  • 3
  • 29
  • 57
  • If you mean this line of code, export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" , it is already added and I did source ~/.bashrc but the one it outputs is v4.2.6 from /usr/bin/node --version, instead of the v6.8.0 one installed with nvm, that means it doesn't solve the problem – Raymond Pang Nov 03 '16 at 16:30
  • 1
    @RaymondPang ok good, then you probably just need to run intellij from the command line, so that those environment variables are within intellij's environment. – Christian Grabowski Nov 03 '16 at 18:13
  • 2
    @RaymondPang Christian is right, your issue is that NVM's env vars aren't being passed to intellij due to is not being a child process of bash. See http://linuxcourse.rutgers.edu/rute/node12.html section 9.9 for details –  Nov 03 '16 at 18:22
  • @Christian Grabowski I typed /opt/idea-IU-162.2032.8/bin/idea.sh in Terminal to launch intelliJ, Is this exactly what you mean by "running from the command line"? After that, I still cannot get the same environment variable and Node in intelliJ. – Raymond Pang Nov 05 '16 at 13:48
0

I had the same issue, and fixed it by pointing to /usr/local/bin/bash instead of /bin/bash

Wildhammer
  • 1,784
  • 1
  • 23
  • 25
0

I had the same issue in mac OS catalina, fixed it by updating Shell path to /bin/zsh in Preferences -> Tools -> Terminal -> Application Settings.

0

On Windows this works for me... enter image description here

HeyMan
  • 1,081
  • 11
  • 26
0

I'm using webstorm on mac and had to add path variable to ~/.zshenv (which is the zsh version of ~/.bash_profile):

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
chuckieDub
  • 1,619
  • 8
  • 24
  • 44