如何在 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如何创建一个?

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

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

如果你在"scripts"下查看,你会看到你的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"
},

最新更新