首页 > 解决方案 > 如何在 NodeJs 项目中添加构建脚本和测试

问题描述

我创建了一个基本的待办事项应用程序,其package.json文件为:

{
  "name": "to-do-app",
  "version": "1.0.0",
  "description": "A basic to-do app created using JavaScript.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sahil Silare",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.19.0",
    "build": "^0.1.4",
    "ejs": "^2.7.1",
    "express": "^4.17.1",
    "npm-build": "0.0.1"
  },
  "devDependencies": {},
  "repository": {
    "type": "git",
    "url": "git+https://github.com/sahil9001/to-do-app.git"
  },
  "keywords": [
    "todo",
    "app"
  ],
  "bugs": {
    "url": "https://github.com/sahil9001/to-do-app/issues"
  },
  "homepage": "https://github.com/sahil9001/to-do-app#readme"
}

每当我运行npm test它时说没有指定测试失败,我该如何解决这个问题?同样,每当我尝试使用TRAVIS CI它时,它都无法检测到build脚本,我该如何创建一个?

标签: javascriptnode.jsnpmbuild

解决方案


在 package.json 的 scripts 属性中指定所有必需的脚本,如下所示

{
      "name": "to-do-app",
      "version": "1.0.0",
      "description": "A basic to-do app created using JavaScript.",
      "main": "index.js",
      "scripts": {
        "test": "put test command here",  // example "test": "mocha test.js"
         "build" : "put build command here"
      },
      "author": "Sahil Silare",
      "license": "MIT",
      "dependencies": {
        "body-parser": "^1.19.0",
        "build": "^0.1.4",
        "ejs": "^2.7.1",
        "express": "^4.17.1",
        "npm-build": "0.0.1"
      },
      "devDependencies": {},
      "repository": {
        "type": "git",
        "url": "git+https://github.com/sahil9001/to-do-app.git"
      },
      "keywords": [
        "todo",
        "app"
      ],
      "bugs": {
        "url": "https://github.com/sahil9001/to-do-app/issues"
      },
      "homepage": "https://github.com/sahil9001/to-do-app#readme"
    }

您没有在 scripts 属性中列出任何脚本命令,并且只有默认值。您必须根据需要创建和放置它。有关更多详细信息,请参阅以下链接

https://www.freecodecamp.org/news/introduction-to-npm-scripts-1dbb2ae01633/

https://flaviocopes.com/package-json/


推荐阅读