?
获取错误 TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for D:\Projects\matri-site\bin\www for Node Js v14
参考解决方案:Question Getting error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for D:\Projects\matri-site\bin\www for Node Js v14https://www.titanwolf.org/Network/q/b40d188b-6070-4b0a-86d3-a9cd105e0b9b/y
javascript - Getting error TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for D:\Projects\matri-site\bin\www for Node Js v14 - Stack Overflowhttps://stackoverflow.com/questions/61536473/getting-error-typeerror-err-unknown-file-extension-unknown-file-extension?
要Node兼容ES6的【import导入】则package.json必须添加【"type": "module"】而如果设置为ES6的module特性,则node执行的后缀必须为js格式的文件【比如:node www.js】而【node www】是失败的,且提示为【TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ""】
解决办法:而Express默认的/bin/www的文件是不带后缀的js文件,因为其中内部包含了【#!/usr/bin/env node】声明,则默认为js文件,为此仅需吧www修改为www.js即可!
?
{
"name": "server_express",
"version": "0.0.0",
"private": true,
"//": "注释:这里的type必须配置module类型才能在后端nodejs运行import语句",
"type": "module",
"scripts": {
"online-start": "node --experimental-json-modules ./bin/www.js",
"nodemon": "npx nodemon --experimental-json-modules nodemon-debug-app.js"
},
"dependencies": {
"async-constructor": "^0.4.17",
"body-parser": "^1.19.0",
"connect-timeout": "^1.9.0",
"cookie-parser": "^1.4.6",
"debug": "~4.3.2",
"express": "~4.17.1",
"express-validator": "^6.13.0",
"http-errors": "^1.8.1",
"morgan": "^1.10.0",
"mysql2": "^2.3.3",
"nodemon": "^2.0.15",
"sequelize": "^6.6.2"
}
}
?
|