错误 Firebase cli 云功能部署。 npm 错误!函数@ lint 脚本失败

Posted

技术标签:

【中文标题】错误 Firebase cli 云功能部署。 npm 错误!函数@ lint 脚本失败【英文标题】:Error Firebase cli cloud function deploy. npm ERR! Failed at the functions@ lint script 【发布时间】:2021-11-26 08:58:19 【问题描述】:

我正在尝试使用 firebase cli 将打字稿云功能部署到谷歌云。我这样做的原因是因为我有一个 javascript 函数,它给了我关于估计 firebase 环境变量的警告。所以使用 firebase cli 是可行的。

现在我在打字稿中有另一个功能。

npm: 6.14.15 火力基地:9.19.0 节点:14.17.6

这里是代码。

"use strict";

import cbor = require("cbor");

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import iot = require("@google-cloud/iot");
const client = new iot.v1.DeviceManagerClient();

// start cloud function
exports.configFirestorev2 = functions
    .region("europe-west1")
    .firestore
    .document("Configs/deviceId")
    .onWrite(async (change: functions.Change<admin.firestore.DocumentSnapshot>,
        context?: functions.EventContext) => 
      if (context) 
        console.log(context.params.deviceId);
        const request = generateRequest(context.params.deviceId, change.after.data(), false);
        return client.modifyCloudToDeviceConfig(request);
       else 
        throw (Error("no context from trigger"));
      
    );

function generateRequest(deviceId: string, configData: any, isBinary: boolean) 
  const formattedName = client.devicePath(process.env.GCLOUD_PROJECT!, functions.config().iot.core.region, functions.config().iot.core.registry, deviceId);
  let dataValue;
  if (isBinary) 
    const encoded = cbor.encode(configData);
    dataValue = encoded.toString("base64");
   else 
    dataValue = Buffer.from(JSON.stringify(configData)).toString("base64");
  
  return 
    name: formattedName,
    binaryData: dataValue,
  ;

这里是 .eslintrc.js

module.exports = 
  root: true,
  env: 
    es6: true,
    node: true,
  ,
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: 
    project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  ,
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: 
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
  , 
;

这是 package.json 文件


  "name": "functions",
  "scripts": 
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  ,
  "engines": 
    "node": "14"
  ,
  "main": "lib/index.js",
  "dependencies": 
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1",
    "@google-cloud/iot": "2.0.0",
    "@types/cbor": "^5.0.0",
    "cbor": "^5.0.2"
  ,
  "devDependencies": 
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  ,
  "private": true

当我尝试使用 firebase deploy --only functions 部署它时

我收到错误,请参阅终端日志。

PS C:\firebase\configFirestore\functions> firebase deploy --only functions

=== Deploying to 'pti-tag-copy'...

i  deploying functions
Running command: npm --prefix functions run lint && npm --prefix functions run build

> functions@ lint C:\firebase\configFirestore\functions
> tslint -p tslint.json

'tslint' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `tslint -p tslint.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint 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!     C:\Users\Shabir\AppData\Roaming\npm-cache\_logs\2021-10-06T06_44_06_259Z-debug.log
events.js:377
      throw er; // Unhandled 'error' event
      ^

Error: spawn npm --prefix functions run lint && npm --prefix functions run build ENOENT
    at notFoundError (C:\Users\Shabir\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:6:26)
    at verifyENOENT (C:\Users\Shabir\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:40:16)
    at ChildProcess.cp.emit (C:\Users\Shabir\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:27:25)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess.cp.emit (C:\Users\Shabir\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:30:37)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12) 
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn npm --prefix functions run lint && npm --prefix functions run build',
  path: 'npm --prefix functions run lint && npm --prefix functions run build',
  spawnargs: []


Error: functions predeploy error: Command terminated with non-zero exit code1

这是完整的调试日志

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   '--prefix',
1 verbose cli   'functions',
1 verbose cli   'run',
1 verbose cli   'lint'
1 verbose cli ]
2 info using npm@6.14.15
3 info using node@v14.17.6
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\firebase\configFirestore\functions\node_modules\.bin;C:\Users\Shabir\AppData\Local\cloud-code\installer\google-cloud-sdk\bin;C:\Users\Shabir\AppData\Local\Programs\Python\Python39\Scripts\;C:\Users\Shabir\AppData\Local\Programs\Python\Python39\;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\Program Files\php-8.0.3-Win32-vs16-x64;C:\Program Files\dotnet\;C:\Program Files\Go\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Intel\Intel(R) Memory and Storage Tool\;C:\Program Files\nodejs\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin;C:\Program Files\MinGW\bin;C:\Users\Shabir\AppData\Local\Microsoft\WindowsApps;;C:\Users\Shabir\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Shabir\AppData\Roaming\cabal\bin;C:\ProgramData\chocolatey\lib\ghc\tools\ghc-8.10.4\bin;C:\tools\msys64;C:\Users\Shabir\go\bin;C:\Users\Shabir\AppData\Roaming\npm
9 verbose lifecycle functions@~lint: CWD: C:\firebase\configFirestore\functions
10 silly lifecycle functions@~lint: Args: [ '/d /s /c', 'tslint -p tslint.json' ]
11 silly lifecycle functions@~lint: Returned: code: 1  signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `tslint -p tslint.json`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:400:28)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:400:28)
13 verbose stack     at maybeClose (internal/child_process.js:1055:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
14 verbose pkgid functions@
15 verbose cwd C:\firebase\configFirestore
16 verbose Windows_NT 10.0.22468
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "--prefix" "functions" "run" "lint"
18 verbose node v14.17.6
19 verbose npm  v6.14.15
20 error code ELIFECYCLE
21 error errno 1
22 error functions@ lint: `tslint -p tslint.json`
22 error Exit status 1
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

【问题讨论】:

你能分享你的 package.json 吗?看起来你是在 Windows 环境中运行的。看起来你有这种issue,建议修改 package.json 中“build”脚本中的路径以使用“\\”而不是“/”。 我已经添加了包文件。请参见上文。 【参考方案1】:

我未能重现您的问题,但我能够按照以下步骤在 Debian 环境中部署您的功能:

    我运行以下命令firebase init functions(我选择了 TS 并在所有选项中选择了是) cd functions npm install cbor npm install @google-cloud/iotfunctions/src/index.ts目录中复制了你的函数 我修改了.eslintrc.js。 我跑firebase deploy --only functions

.eslintrc.js 中,我在规则部分添加了以下几行:

"max-len": ["error", "code": 200],
"require-jsdoc": 0,

这是最后的.eslintrc.js

module.exports = 
  root: true,
  env: 
    es6: true,
    node: true,
  ,
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: 
    project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  ,
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: 
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
    "max-len": ["error", "code": 200],
    "require-jsdoc": 0,
  ,
;

您能否验证您是否遵循了这些步骤?

编辑

如 cmets 中所述。按照answer 中的步骤解决了这个问题。总之,OP 所做的就是在.eslintrc.js 文件中将tsconfigRootDir 设置为__dirname

module.exports = 
  // ...
  parserOptions: 
    project: "tsconfig.json",
    tsconfigRootDir: __dirname,
    sourceType: "module",
  ,
  // ...

【讨论】:

感谢您的回复。我按照你的描述做了,但我仍然遇到错误 我创建了一个新目录并按照你说的做了。我仍然遇到错误,然后我发现无法识别 tsconfig.json 路径存在问题。并按照此处的步骤进行操作。-> ***.com/questions/64933543/… 我了解到您通过遵循 answer 将 tsconfigRootDir 设置为 __dirname 来解决此问题,对吗? 是的,这是正确的。所以简而言之,我首先做了你所说的,然后我得到了另一个警告,我通过将 tsconfigRootDir 更改为 __dirname 来修复。

以上是关于错误 Firebase cli 云功能部署。 npm 错误!函数@ lint 脚本失败的主要内容,如果未能解决你的问题,请参考以下文章

Cors - Firebase 云功能中的访问控制允许来源错误

在 Firebase Cloud 功能中启用 Cors

为 Firebase 云功能启用 Cors

未为 Firebase 云功能启用 Cors

Firebase 云功能:带有 CORS 阻止的总线男孩的 POST 方法

由于 CORS 限制,无法使用 firebase 在本地进行测试