Jump to content

Issues running NPX command in terminal via Node JS


Recommended Posts

I'm trying to create a workflow for creating a Create React App template. A new extended version of this question I asked, wasn't sure if I should comment on that one or create a new post, so sorry if this is repetitive!

 

The process should be:

  1. Create folder inside Codes with the name passed in arguments
  2. Go inside that folder with the terminal, and run this command:
    npx create-react-app ./ --template typescrip
  3. Once the nix command finishes, delete the src folder created in the project, and copy the template src folder (my react boilerplate template I have in my workflow folder) to the project.
  4. Open in VS Code

 

This is my NodeJS code right now, commented out step 3 as I first want to get my npx command to work.

import { spawn } from 'child_process'
import alfy from 'alfy'
import fs from 'fs-extra'
import os from 'os'

async function run() {
  const homePath = os.homedir()
  const dirName = alfy.input
  const fullPath = homePath + '/Codes/' + dirName

  if (fs.existsSync(fullPath)) return alfy.output([{ title: 'Folder already exists' }])
  await fs.promises.mkdir(fullPath)

  const ls = spawn(`cd ${fullPath} && npx create-react-app ./ --template typescript`, {
    shell: '/bin/zsh',
  })

  ls.stdout.on('data', data => {
    console.log(`stdout: ${data}`)
  })

  ls.stderr.on('data', data => {
    console.log(`stderr: ${data}`)
  })

  ls.on('error', error => {
    console.log(`error: ${error.message}`)
  })

  ls.on('close', code => {
    console.log(`child process exited with code ${code}`)
  })

  // fs.rmdirSync(fullPath + '/src', { recursive: true, force: true })
  // fs.mkdirSync(fullPath + '/src')

  // fs.copySync('src', fullPath + '/src')
}

run()

 

The error inside the Alfred debugger: stderr: zsh:1: command not found: npx

New to child_process in NodeJS, so I'm sure it's something obvious I'm missing!

 

Hope that makes sense, any help is appreciated! ☺️

Link to comment
2 hours ago, NorahMaria said:

The error inside the Alfred debugger: stderr: zsh:1: command not found: npx

New to child_process in NodeJS, so I'm sure it's something obvious I'm missing!


That’s the same error, with the same cause and same solution. Use the full path to the tool.

 

And be sure to check what I linked you to if you still don’t understand the cause.

 

Your previous code was better. Zsh is ideal for this sequence of steps; switching to node needlessly complicates it and makes it less portable. You’re invoking a shell from your node code anyway, so the switch is all disadvantages.

Edited by vitor
Link to comment

@vitor thanks for your help, and sorry, I did not realise this was the same issue! The reason I am switching it is to allow for feedback on the process of it in Alfred, rather than running the command and Alfred going away, and then waiting for some minutes to see if it opens or not - I want some feedback in Alfred for 'created folder', 'creating app', 'created' etc, and I know how to do that in NodeJS, if that makes sense!

Link to comment
38 minutes ago, NorahMaria said:

sorry, I did not realise this was the same issue!

 

No need to apologise, but perhaps help me understand what your steps were when you bumped into the new problem. What do you think stopped you from realising it was the same problem? Was the first explanation not clear enough for your mental model?

 

7 minutes ago, NorahMaria said:

I want some feedback in Alfred

 

You mean feedback in the debugger? That’s also easier in Zsh, append >&2 to your command to redirect STDOUT to STDERR: your_command >&2.

 

11 minutes ago, NorahMaria said:

and I know how to do that in NodeJS, if that makes sense!

 

It does, I understand you’re using what you know. But in this case the alternative is really simple (literally three characters) and superior in every regard. You had already done most of the work!

Link to comment
39 minutes ago, vitor said:

You mean feedback in the debugger? That’s also easier in Zsh, append >&2 to your command to redirect STDOUT to STDERR: your_command >&2.

No, I mean feedback to the user from the Alfred Window, like items / replies. I'm sorry I don't know the lingo for it! Like for example, when I press enter and it starts creating an app - Alfred shows a lil loading indicator, and once it finishes it, the Alfred window again updates to say 'Done' or something. Hope that clarified it!

 

Link to comment
19 hours ago, NorahMaria said:

Alfred shows a lil loading indicator, and once it finishes it, the Alfred window again updates to say 'Done' or something.

 

That’s basically static JSON, which can easily be emitted from a shell script, too. Node does seem like overkill for this, but seeing as it’s an npx workflow, so Node needs to be installed anyway, why not? JavaScript is easier to read than shell.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...