小文章-简单讲解json核心属性

188 阅读1分钟

讲解一下json文件中的核心属性

main和script属性

1.main属性

指定文件的入口文件

这个很简单就不需要做详细的解答了

如果不配置main属性,默认会把当前目录下的index.js作为入口文件

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

const test = require('test');
test.test()

也可以设置其他的入口文件就可以直接设置

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

2.script属性

指定自定义指令脚本

在node中我们是不是经常需要进行传参

可以通过console.log(process.argv)来拿到传入的参数

console.log(process.argv);

经常在开发中可能会一直进行传参,而且传给还是同一个参数,是不是要输那么长的代码就很傻

这个时候就可以配置script属性来进行简化操作

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "node main.js name=sandy age=21"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

这样是不是就简化了操作

如果自定义指令是test或者start的话,就可以省略run这个词,直接npm test或者npm start

npm test
npm start
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "node main.js name=sandy age=21"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "node main.js name=sandy age=21"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

是不是就没有问题了