0

I have created a simple CLI to bootstrap projects with inquirer and have successfully published it to NPM. However, when installing it with

npm i -g noobject

it's successfully loading and installing. When running

noobject

in the cmd line, it does return "command not found" and when running

npx noobject its returning the following.
npm ERR! could not determine executable to run

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Me\AppData\Local\npm-cache\_logs\2022-02-13T17_28_00_374Z-debug-0.log

I have tried it both on windows 10 and linux ubuntu 20.4. Here is my index.js: https://sourceb.in/ZGZMWKU8tb

#! /usr/bin/env node
const inquirer = require("inquirer");
const fs = require("fs");

// FULL SOURCE TREE: https://github.com/vKxni/noobject

const CURR_DIR = process.cwd();
const CHOICES = fs.readdirSync(`${__dirname}/templates`);

// Questions asked to the User
const QUESTIONS = [
  {
    name: "project-choice",
    type: "list",
    message: "What project would you like to generate?",
    choices: CHOICES,
  },
  {
    name: "project-name",
    type: "input",
    message: "Project name:",
    validate: (input) => {
      if (/^([A-Za-z\-\_\d])+$/.test(input)) return true;
      else
        return "Project name may only include letters, numbers, underscores and hashes.";
    },
  },
];

// Send a prompt to choose a template
inquirer.prompt(QUESTIONS).then((answers) => {
  const projectChoice = answers["project-choice"];
  const projectName = answers["project-name"];
  const templatePath = `${__dirname}/templates/${projectChoice}`;

  // Create the folder with the project name choosed by the user
  fs.mkdirSync(`${CURR_DIR}/${projectName}`);

  createDirectoryContents(templatePath, projectName);

  console.log(`✅ Successfully created ${projectName}`);
});

const createDirectoryContents = (templatePath, newProjectPath) => {
  const filesToCreate = fs.readdirSync(templatePath);

  filesToCreate.forEach((file) => {
    const origFilePath = `${templatePath}/${file}`;

    const stats = fs.statSync(origFilePath);

    if (stats.isFile()) {
      const contents = fs.readFileSync(origFilePath, "utf8");

      if (file === ".npmignore") file = ".gitignore";

      const writePath = `${CURR_DIR}/${newProjectPath}/${file}`;
      fs.writeFileSync(writePath, contents, "utf8");

      console.log(`⚠️ Created ${writePath}`);

    } else if (stats.isDirectory()) {
      fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`);

      createDirectoryContents(
        `${templatePath}/${file}`,
        `${newProjectPath}/${file}`
      );
    }
  });
};

Here is my package.json:

{
  "name": "noobject",
  "version": "1.2.8",
  "description": "Project generator written in NodeJS",
  "main": "index.js",
  "scripts": {
    "noobject": "node index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/vKxni/noobject.git",
  "keywords": [
    "node",
    "generator",
    "project",
    "noobject"
  ],
  "author": "vKxni",
  "license": "ISC",
  "dependencies": {
    "inquirer": "^8.2.0"
  },
  "bin": {
    "noobject": "index.js"
  }
  }
}

(As a pastebin: https://sourceb.in/CF4ydtNccG). I hope anyone can help me so I can bootstrap projects and config files easier and faster.

torek
  • 448,244
  • 59
  • 642
  • 775

1 Answers1

2

Your problem is that you're missing a } for repository, so bin is actually inside repository. You can see there's a doubled } in the second to last line.

{
  "name": "noobject",
  "version": "1.2.8",
  "description": "Project generator written in NodeJS",
  "main": "index.js",
  "scripts": {
    "noobject": "node index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/vKxni/noobject.git",
  /* <----- should be here */
  "keywords": [
    "node",
    "generator",
    "project",
    "noobject"
  ],
  "author": "vKxni",
  "license": "ISC",
  "dependencies": {
    "inquirer": "^8.2.0"
  },
  "bin": {
    "noobject": "index.js"
  }
  } /* <----- wrong place! */
}

If you fix that, it works:

{
  "name": "noobject",
  "version": "1.2.8",
  "description": "Project generator written in NodeJS",
  "main": "index.js",
  "scripts": {
    "noobject": "node index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/vKxni/noobject.git",
  },
  "keywords": [
    "node",
    "generator",
    "project",
    "noobject"
  ],
  "author": "vKxni",
  "license": "ISC",
  "dependencies": {
    "inquirer": "^8.2.0"
  },
  "bin": {
    "noobject": "index.js"
  }
}
Leonardo Dagnino
  • 2,914
  • 7
  • 28