【问题标题】:Nodemon ''npm' is not recognized as an internal or external commandNodemon ''npm' 未被识别为内部或外部命令
【发布时间】:2018-07-13 13:05:01
【问题描述】:

我意识到这很可能是一个重复的问题。我是 nodemon 的新手,我正在尝试使用 nodemon 为 Vue JS 项目建立服务器。我正在尝试使用 nodemon 运行 eslint,但无法弄清楚为什么我不断收到错误消息。如果我在 --exec 之后删除 npm,它会告诉我 ''run' 无法识别,如果我删除它,我会得到 ''lint' 无法识别等等。 我的 package.json 文件:

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon src/app.js --exec 'npm run lint && node'",
    "lint": "eslint **/*.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "eslint": "^4.16.0",
    "nodemon": "^1.14.12"
  }
}

我也在我的启动脚本中尝试过这段代码:

   "scripts" : {
      "start": "./node_modules/nodemon/bin/nodemon.js src/app.js --exec 'npm run lint && node'",
      "lint": "./nodemodules/.bin/eslint **/*.js"
    } 

在哪里告诉我“。”不被识别为内部外部命令。 我已将 nodemon 安装到我的服务器文件夹和项目目录以及全局中。我也对 eslint 做了同样的事情。

【问题讨论】:

    标签: node.js eslint nodemon


    【解决方案1】:

    全局安装它以使其在路径上可用。

    npm i -g nodemon 
    or if using yarn
    yarn global add nodemon
    

    如果您尝试过这种方法但它不起作用。

    您应该尝试在本地运行它..
    你必须像这样在你的 package.json 中创建一个脚本

    "script": {
        "server" : "nodemon scriptFile.js" //name of the file you want to run
    }
    

    然后使用,

    npm run server
    

    但在此之前,
    在本地安装 nodemon。 检查它,如果它在 package.json 上可用

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。
      出于某种原因,您不能在 npm 脚本中使用简单引号。
      请改用转义双引号。这应该有效:

      "start": "nodemon src/app.js --exec \"npm run lint && node\""
      

      【讨论】:

        【解决方案3】:

        我今天也遇到了同样的问题。做了一些谷歌的东西,发现这不再起作用了。所以我尝试了这个

        "scripts": {
        "prestart": "npm run lint ",
        "start": "nodemon src/app.js ",
        "lint": "./node_modules/.bin/eslint src/*.js"
        },
        

        当您npm start 时,节点将在启动脚本之前运行预启动脚本。一旦文件被更新,此预启动将不会由 nodemon 运行。因此,我们必须调用 nodemon 事件。所以创建一个nodemon.json 在根文件夹上并粘贴以下内容。

            {
         "events": {
             "restart": "npm run lint"
        
           }
        }
        

        你可以从这里nodemon config 阅读更多的nodemon 配置选项。还有更多的nodemon 事件。你可以从这里event restart 阅读它们

        PS:我对此很陌生。 :)

        EDIT1:

        您可以如下使用。这不需要 nodemon 配置;

        "scripts": {
        "start": "node src/app.js",
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "nodemon src/app.js --exec \"npm run lint --fix && node\"",
        "lint": "eslint --fix **/*.js "
        }
        

        运行使用npm run dev 它将运行 es lint + nodemon。这是针对 windows cmd 命令的。如果您使用的是 bash 终端,请删除 "dev" 中的 \

        "dev": "nodemon src/app.js --exec "npm run lint --fix && node""

        【讨论】:

        • 这很有效并且非常有用。非常感谢。
        • @Glenn 很高兴听到这个消息 :)
        猜你喜欢
        • 2018-10-19
        • 2021-04-25
        • 2019-06-28
        • 2016-11-29
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多