Execute node.js npm package from node:child_process using `spawn` function

102 views Asked by At

I'm building an obsidian plugin. I'm able to get a callback for node.js function that run in electron. I want this command will go to a specific path and run 'npm run slidev' in a configured path. I assume the package 'slidev is installed in a project in the configured directry (like running 'npm i' in a folder that has this dependency in 'package.json'), and the user already installed node.js. If the users runs this script in the terminal - slidev plugin works. I want it to work with the 'spawn' function

when I try this in my node.js function - this function fails - it does not know where node.js is. Expand on the problem Show what you've tried, tell us what napened, and why it dichy met your needs. Not all questions benefit from including code, but if your problem is better understood with code you've written, you should include a minimal, reproducible example. Please make sure to post code and errors as text directly to the question (and not as images), and format them appropriately.

const codeBlockContent = [|
// This makes node & npm usable
config.initialScript,
// If you use nom scripts, don't forget to add -- after the npm command:
npm run slidev §{slidePathRelativeToTemplatePath} — --port ${config.port},
return spawn|codeBlockContent, Il, f env: process.env, shell: true,
// cwd: templatePath,
});

At the past, in my second computer - by configuring 'initialScript to source $HOME/.profile' I was able to get it working. I wonder which script is missing here? I can run "echo "HI"', but I cannot get the node.js version. BTW - I'm using fnm as node version manager. I'm not sure what is the order of executions in the terminal compare to node.js process.

Thanks!

0

There are 0 answers