node-sass运行报ERR_INVALID_ARG_TYPE path参数类型错误排查
node-sass运行报path参数类型错误排查
报错日志
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined at new NodeError (node:internal/errors:372:5) at validateString (node:internal/validators:120:11) at Object.extname (node:path:837:5) at getOptions (C:\Users\skh43\Desktop\Тест сайт\node_modules\node-sass\bin\node-sass:228:40) at Object.<anonymous> (C:\Users\skh43\Desktop\Тест сайт\node_modules\node-sass\bin\node-sass:408:15) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:981:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) { code: 'ERR_INVALID_ARG_TYPE' } Waiting for the debugger to disconnect... PS C:\Users\skh43\Desktop\Тест сайт>
问题根源
- 核心原因是
package.json里的scss编译脚本参数写错:watch模式的正确参数为双横杠开头的--watch(或单横杠加单字符缩写-w),你写的-watch属于参数格式错误,node-sass会将其解析为多个单字符参数的组合,无法正确识别传入的scss源目录路径,最终在调用path.extname方法时拿到undefined值,触发参数类型错误。 - 次要排查点:需确认项目根目录下存在手动创建的
scss源文件目录、css输出目录,目录缺失也会导致路径读取异常。
修复步骤
- 修改
package.json中scripts字段下的scss命令,修正watch参数格式:
{ "name": "test_site", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "scss": "node-sass --watch scss -o css" }, "author": "me", "license": "ISC", "dependencies": { "node-sass": "^7.0.1", "react-scripts": "^5.0.1" } }
- 检查项目根目录,若不存在
scss、css文件夹则手动创建。 - 重新执行
npm run scss即可正常启动scss监听编译。
补充说明:node-sass已停止维护多年,若后续出现Node版本兼容问题,可直接替换为官方继任的dart-sass(包名为
sass),替换后上述编译命令无需大幅修改即可正常运行。
内容的提问来源于stack exchange,提问作者Тв0р0бушек
相关产品推荐
相关产品推荐

