2

I'm editing Typescript files in VS Code, and when I invoke the build task via Shift-Cmd-B, I get

`env: node: no such file or directory'

but if I start the terminal and manually type the same command it's using, which is

tsc -p /Users/Mike/Sync/projects/teaching/blendoku/tsconfig.json

it works fine. What is different about the shell that is launched with the build and watch tasks?

composerMike
  • 966
  • 1
  • 13
  • 30

2 Answers2

4

I researched this problem for some time, and here is what I found:

  1. This StackOverflow answer is quite informative:

This happens because .bashrc is not loaded for tasks, as they are non-interactive shells. You need to have the nvm shell enhancements loaded for non-interactive shells as well to use global packages installed via nvm to be available to VS Code.

Create a new text file named, say, nvm-autoload.sh and place it inside /etc/profile.d/ folder to run it for all login shells, which includes the task shells for VS Code. Add the following to that file:

# Enable nvm if available
if [ -f ~/.nvm/nvm.sh ]; then
  source ~/.nvm/nvm.sh
fi

You may need to logout and log back in for this to take effect.

  1. If you use VS Code on macOS you can also try:

    • You can try exporting path/to/node in ~/.bash_profile, details in this thread: GitHub issue

    • You can try setting terminal.integrated.shellArgs.osx to [], details in this thread: GitHub issue

    • If nothing else works, this stackoverflow thread might help you: StackOverflow question

  2. If you use VS Code on Windows:

    • The issue could be tied to path separators in build config being escaped/stripped out prior to build command being run; for more details and steps how to fix check this StackOverflow answer
  3. It is also worth noting that VS Code's TypeScript language service is separate from your installed TypeScript compiler. It could be the cause of some discrepancies. Link

  4. Also some general advice is to make sure that tsconfig.json contents are valid, and that the path to it has no spaces.

curiousMinded
  • 336
  • 3
  • 16
0

Sounds to me like you need to make a .env file, if you want to run npm command from commands line in VS code, the easiest way is to add them to your package.json that was created in your project when you added node.

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "pm2-dev app.js" // this has no meaning for your issue just an 
                               example
  },

since you are trying to build

"scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "start": "pm2-dev app.js",
    "build": "your program to run here"
    },

Then you can run your build npm run build at VS terminal, but, as stated above, from your error, it sounds like you need a .env file.

tim
  • 677
  • 9
  • 11
  • I don't think the VS Code Typescript "build task" has anything to do with npm. It's one of the tasks that VS Code knows how to do right out of the box with no configuration. – composerMike Oct 22 '19 at 17:36