Search code examples
node.jsnpmcommand-linecommand-line-interface

NPM Command Line Tool - Command not working


I'm working on a CLI tool. I'm using macOS. I was using node index.js <command> when running it locally. I decided to publish it as an npm package, but the command I've defined in the bin section of my package.json won't work.

In my package.json, i've defined bin as follows:

  "bin": {
    "scaffold": "index.js"
  },

I'm really not sure how to navigate this and quite unfamiliar. Would appreciate any help.

I've installed the package globally.

Tried to troubleshoot by running a few commands from posts, here are the results if they help:

Running npm bin -g results in:

Unknown command: "bin"

To see a list of supported npm commands, run:
  npm help

Running npm root -g results in:

/opt/homebrew/lib/node_modules

Running which scaffold results in:

/opt/homebrew/bin/scaffold

Running npm list -g project-scaffold results in:

/opt/homebrew/lib
└── project-scaffold@1.1.1

Here's the package with the link to the repo and everything: https://www.npmjs.com/package/project-scaffold

Thank you.


Solution

  • Figured it out. Had a dumb if statement manually checking the first index of the argv array for the file path. This wouldn't work if switching from node index.js <command> to scaffold <command> because the first index no longer points to what the if statement is looking for, thereby never allowing runCLI() to run. Shame on me.

    The culprit:

    if (process.argv[1] === fileURLToPath(import.meta.url)) {
      runCLI();
    }