Reputation: 6753
According to docs it is supposed to work. When I press Ctrl-Shift-B, I do get npm: build
as an option, which if I run I get npm command not found
in the terminal. However, manually running npm run build
works.
I am using nvm
. Some relevant outputs:
~ which node
/home/gt/.nvm/versions/node/v10.15.3/bin/node
~ which npm
/home/gt/.nvm/versions/node/v10.15.3/bin/npm
~ echo $NODE_PATH
/home/gt/.nvm/versions/node/v10.15.3/bin/node
What could be the reason for this? And how to fix it?
Upvotes: 1
Views: 3951
Reputation: 11347
We solved this by sourcing file where NVM is setup and then running npm commands.
Example:
"command": "source scripts/nvm && npm run develop",
# ./scripts/nvm
# This is for users who don't have a global Node.js installation but instead
# rely on NVM. This file is sourced when running build tasks in non-login
# shells.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Alternatively you can source ~/.bash_profile (or any other file) here. We feel this is a better alternative than hardcoding nvm's npm path.
Upvotes: 1
Reputation: 684
I am using nvm and wrote a wrapper script that reads the node version from .nvmrc
. This avoids hardcoding a specific node version in multiple places. The wrapper script is configured for all shell commands in settings.json
using the terminal.integrated.automationShell.<os>
setting.
#!/bin/bash
set -e
if [[ "$2" == nvm* ]]; then
export NODE_VERSION=v$(cat .nvmrc)
if [ -z "$NODE_VERSION" ]; then
echo "$(pwd)/.nvmrc does not exist or is empty"
exit 1
fi
CLEAN_CMD=$(echo $2 | sed -e 's/nvm/npm/g')
/bin/bash -c "~/.nvm/nvm-exec $CLEAN_CMD"
else
/bin/bash -c $@
fi
A task can then use the correct node version using the nvm <command>
command:
{
"type": "shell",
"command": "nvm run compile",
"label": "npm-compile",
"problemMatcher": "$tsc",
"isBackground": false,
"presentation": {
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
}
}
There is also a gist that provides a little bit more information: https://gist.github.com/fvclaus/540c08921ba884fef959053c4f974bfc
Upvotes: 1
Reputation: 6753
Self-answering because I finally found the two things I needed to do:
Create a tasks.json as explained in one answer:
{
"version": "2.0.0",
"tasks": [
{
"label": "npm:build",
"type": "shell",
"group": { "kind": "build", "isDefault": true },
"command": "/home/gt/.nvm/versions/node/v10.15.3/bin/npm run build"
}
]
}
After this I got another error saying: "/usr/bin/env: node: No such file or directory"
Then I found this github thread, and so I created symlink as per suggestion:
$ sudo ln -s /home/gt/.nvm/versions/node/v10.15.3/bin/node /usr/bin/node
and the ctrl+shift+b
shortcut works now. Note that the source argument in your case can be got from which node
.
Upvotes: 1
Reputation: 286
Try File -> Open Folder, opening your npm project folder and run your build command from there. If that fails, you may need to update your tasks.json to look similar to
{
"version": "0.0.1",
"tasks": [
{
"label": "npm:build",
"type": "shell",
"group": { "isDefault": true,"kind": "build", },
"command": "npm run build"
}
]
}
Upvotes: 0
Reputation: 3444
The issue is likely because npm isn't in VS Code's path (likely /usr/bin
). However, you can create a custom build task and specify the path. In the .vscode
directory, create a tasks.json
file. Place the contents below in the file.
{
"version": "2.0.0",
"tasks": [
{
"label": "npm:build",
"type": "shell",
"group": { "kind": "build", "isDefault": true },
"command": "/home/gt/.nvm/versions/node/v10.15.3/bin/npm run build"
}
]
}
The group setting makes this task the default and a build-style task in VS Code. Thus, it enables it to be launched from ctrl+shift+b
.
Upvotes: 1
Reputation: 4945
I think you are not in the right directory.
Try the below method.
Open any ts/html file from any component the do the same (Ctrl+Shift+B).
Probably it will work, it's work for me.
Still you face the same problem please share the snapshot.
Upvotes: 0