package.json 是否可写成这样呢"type": "commonjs"||"module"? 到时候2种都可以使用?
npm的package.json的 type字段,我们知道:一般可以填写:commonjs
或 module
{
"name": "demo-01",
"version": "1.0.0",
"description": "",
"main": "main.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"author": "",
"license": "ISC",
"devDependencies": {
...
}
}
请问下:1.除了前面2种值,还有其他的值的可能吗?2.是否可写成这样呢"type": "commonjs"||"module"? 到时候2种都可以使用。
回复
1个回答

test
2024-07-12
不行嗷,只能二选一,文档里只提到了 commonjs
和 module
两个值。
不过你可以通过后缀来区分。
Regardless of the value of the"type"
field,.mjs
files are always treated asES modules
and.cjs
files are always treated asCommonJS
.不管"type"
字段的值是多少,.mjs
文件总是被视为ES modules
,.cjs
文件总是被视为CommonJS
。
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容