Cloud FuntionsをTypeScriptで書いているプロジェクトで firebase deploy --only functions
を実行した際に、親ディレクトリの node_modules
が参照されてしまいTypeScriptのコンパイルエラーに悩まされた。
この現象が起こった時のfirebase-toolsのバージョンは v6.9.1
。
$ firebase deploy --only functions
=== Deploying to ’some-project-name'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /Users/****/path/to/functions
> tslint --project tsconfig.json
Running command: npm --prefix "$RESOURCE_DIR" run build
> functions@ build /Users/****/path/to/functions
> tsc
../node_modules/@babel/parser/typings/babel-parser.d.ts(11,70): error TS1144: '{' or ';' expected.
../node_modules/@babel/parser/typings/babel-parser.d.ts(16,80): error TS1144: '{' or ';' expected.
../node_modules/@types/jest/index.d.ts(384,32): error TS1005: ';' expected.
../node_modules/@types/jest/index.d.ts(384,33): error TS1003: Identifier expected.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/yuhiisk/.npm/_logs/2019-05-09T12_47_39_299Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code2
エラーコードには ../node_modules/@babel/parser/typings...
と親ディレクトリのnode_modulesが参照されてしまっている。
解決方法
Cloud Functionsを格納しているディレクトリ(大体が functionsだと思う)のtsconfig.json
内のcompilerOptionsに typeRoots
プロパティを追加して@typesの場所を明示する。
{
"compilerOptions": {
"lib": ["es6"],
"module": "commonjs",
"noImplicitReturns": true,
"outDir": "lib",
"sourceMap": true,
"target": "es6",
"typeRoots": [
"./functions/node_modules/@types" // ← functions内のnode_modules/@typesを指定する
]
},
"compileOnSave": true,
"files": [
"node_modules/typescript/lib/lib.es6.d.ts"
],
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
参考:Functions only deploy erroring, referencing parent application node_modules folder
上記のIssueではcompilerOptionsの skipLibCheck
をtrueにする方法も提示されているけど、型定義ファイルのエラーに対しての処置としては本質的ではないので typeRoots を指定するほうが良い。