【问题标题】:NPM CLI returns command not foundNPM CLI 返回命令未找到
【发布时间】:2022-02-14 11:58:05
【问题描述】:

我创建了一个简单的 CLI 来使用查询器引导项目,并已成功将其发布到 NPM。但是,在安装时使用

npm i -g noobject

它已成功加载和安装。 运行时

noobject

在 cmd 行中,它确实返回“找不到命令”并且在运行时

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

我在 Windows 10 和 linux ubuntu 20.4 上都试过了。 这是我的 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}`
      );
    }
  });
};

这是我的 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"
  }
  }
}

(作为 pastebin:https://sourceb.in/CF4ydtNccG)。 我希望任何人都可以帮助我,以便我可以更轻松、更快地引导项目和配置文件。

【问题讨论】:

    标签: npm inquirer inquirerjs


    【解决方案1】:

    您的问题是您缺少repository},所以bin 实际上在repository 中。你可以看到倒数第二行有一个加倍的}

    {
      "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! */
    }
    

    如果你解决了这个问题,它会起作用:

    {
      "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"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 2022-08-22
      相关资源
      最近更新 更多