我在 Angular 官网下载的 library Schematics 例子,运行命令行 npm run build 时,遇到如下错误:
my-lib@0.0.1 build c:\Code\SPA\schematics-for-libraries\projects\my-lib
…/…/node_modules/.bin/tsc -p tsconfig.schematics.json
‘…’ is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-lib@0.0.1 build: ../../node_modules/.bin/tsc -p tsconfig.schematics.jsonnpm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-lib@0.0.1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\I042416\AppData\Roaming\npm-cache_logs\2021-10-18T07_11_32_366Z-debug.log
问题的根源出现在 package.json 这一行:
“build”: “…/…/node_modules/.bin/tsc -p tsconfig.schematics.json”,
windows 不支持通过 …/…/ 去执行当前文件夹上级目录里的某个可执行文件。
调用的其实是这个 tsc 文件:
临时解决方案
在库的 package.json 里增添 TypeScript 的 devDependencies 依赖:
然后使用库当前目录下的 node_modules 里的 tsc 进行编译:
最后问题解决,可以开始编译了。