如何在NodeJs项目中添加构建脚本和测试

时间:2019-10-10 19:40:29

标签: javascript node.js npm build

我创建了一个基本的待办事项应用,其中的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脚本,该如何创建脚本?

2 个答案:

答案 0 :(得分:1)

在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/

答案 1 :(得分:1)

如果您在“脚本”下查看,则会看到npm脚本,例如运行“ npm test”时调用的脚本。

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

运行NPM init时的默认值是此简单回显,表示没有测试,您必须在此处编写自己的测试命令。

Travis无法检测到构建脚本,因为在脚本部分中您没有“ build”的值,请添加以下内容:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "my_script_to_build"
  },