首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NodeJs项目中添加构建脚本和测试

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

Stack Overflow用户
提问于 2019-10-11 03:40:30
回答 2查看 6.6K关注 0票数 1

我已经创建了一个基本的todo应用程序,它的package.json文件如下:

代码语言:javascript
复制
{
  "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脚本,我如何创建一个?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-11 04:00:53

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

代码语言:javascript
复制
{
      "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"
    }

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

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

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

票数 2
EN

Stack Overflow用户

发布于 2019-10-11 04:07:59

如果你在“脚本”下查看,你会看到你的npm脚本,比如你在运行"npm test“时调用的脚本。

代码语言:javascript
复制
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

当你运行NPM init时,默认值是这个简单的回显,告诉你没有测试,你必须在这里写你自己的测试命令。

Travis未能检测到构建脚本,因为您的脚本部分中没有" build“的值,请添加如下内容:

代码语言:javascript
复制
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "my_script_to_build"
  },
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58330038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档