bombillazo
bombillazo4mo ago

Run `nvm` using Deno.Command

Hello, I am trying to run nvm from a Deno script using Deno.Command.
new Deno.Command('nvm', {
args: ['use'],
stderr: 'inherit',
stdin: 'inherit',
stdout: 'piped',
}).outputSync;
new Deno.Command('nvm', {
args: ['use'],
stderr: 'inherit',
stdin: 'inherit',
stdout: 'piped',
}).outputSync;
but I am getting the following error:
Failed to spawn 'nvm': No such file or directory (os error 2)
Failed to spawn 'nvm': No such file or directory (os error 2)
nvm was install with brew and I tried passing env: {PATH: Deno.env.get('PATH') but nothing works...
1 Reply
Deno AI Helper
Deno AI Helper4mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, there seems to be a syntax error. The Deno.Command constructor does not take the command and options as separate arguments. Instead, you should pass the command as the first element of the args array. Also, you are not actually calling the outputSync method; you need to add parentheses to invoke it. Here's a corrected version of your code snippet:
const output = new Deno.Command({
cmd: ['nvm', 'use'], // 'nvm' should be part of the `cmd` array
stderr: 'inherit',
stdin: 'inherit',
stdout: 'piped',
env: { PATH: Deno.env.get('PATH') }, // Make sure to include this if needed
}).outputSync(); // Add parentheses to call the method
const output = new Deno.Command({
cmd: ['nvm', 'use'], // 'nvm' should be part of the `cmd` array
stderr: 'inherit',
stdin: 'inherit',
stdout: 'piped',
env: { PATH: Deno.env.get('PATH') }, // Make sure to include this if needed
}).outputSync(); // Add parentheses to call the method
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. Additionally, nvm is a shell function, and it might not be available as a standalone executable in the PATH, which could be why you are encountering the error. You may need to run the command in the context of a shell that has nvm initialized, such as bash -c 'nvm use'.