无法在模块TypeScript NodeJS Firebase Functions项目之外使用import语句



我最近用TypeScript建立了一个Firebase函数项目。据我所知,我正在使用与过去相同的设置。然而,这一次,当我运行firebase emulators:start时,我收到以下错误:

Error: Error occurred while parsing your function triggers.
<Local Computer Path>/firebase/functions/src/index.ts:3
import functions = require("firebase-functions");
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at loadModule (/opt/homebrew/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js:10:16)
at /opt/homebrew/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js:34:21

据我所知,我没有改变过去的项目,在过去的项目中我没有遇到这个问题。我能想到的唯一潜在区别是,我使用npm install -g firebase-tools@latest更新了我的firebase工具。以下是我的项目中的一些文件:

src/index.ts

/* eslint-disable max-len */
import functions = require("firebase-functions");
import admin = require("firebase-admin");
admin.initializeApp();
...

.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",
tsconfigRootDir: __dirname,
},
ignorePatterns: [
"/lib/**/*", // Ignore built files.
],
plugins: [
"@typescript-eslint",
"import",
],
rules: {
"indent": ["error", 2],
"object-curly-spacing": ["error", "always"],
"quotes": ["error", "double"],
"import/no-unresolved": 0,
},
};

软件包.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": "16"
},
"main": "./src/index.ts",
"dependencies": {
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.18.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^0.2.0",
"typescript": "^4.5.4"
},
"private": true,
"type": "module",
"module": "ES2020"
}

软件包.dev.json

{
"include": [
".eslintrc.js"
]
}

tsconfig.json

{
"compilerOptions": {
"module": "ES2020",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "ES2020"
},
"compileOnSave": true,
"include": [
"src"
]
}

firebase.json

{
...
"functions": {
"predeploy": [
"npm --prefix "$RESOURCE_DIR" run lint",
"npm --prefix "$RESOURCE_DIR" run build"
]
},
...
}

我的src/index.ts导入语法与谷歌在Firestore文档中概述的内容相匹配:导入所需模块并初始化应用程序。

我四处寻找答案,但还没有找到。以下是我尝试过的一些资源:

SyntaxError:无法在模块Firebase Functions 之外使用导入语句

Node.js v13.14.0文档

Typescript:不能在模块之外使用导入语句

提前感谢您的帮助!

原来这个问题有一个简单的解决方案!firebase模拟器不支持typescript。要在模拟器中运行函数,首先需要编译为JS(参考文档(。npm run servefirebase deploy都会自动转换您的代码,firebase emulators:start会自动执行NOT操作。使用模拟器时,必须首先在函数文件夹中运行npm run build

另外请注意,在您的package.json中,您需要main参数来引用已编译的JS代码。我需要更新我的package.jsontsconfig.json。以下是最终版本:

软件包.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": "16"
},
"main": "./lib/index.js",
"dependencies": {
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.18.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^0.2.0",
"typescript": "^4.5.4"
},
"private": true
}

tsconfig.json

{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src"
]
}

您需要使用const functions = require("firebase-functions");而不是import functions = require("firebase-functions");-下面的第二个导入也是如此。'import'关键字只能与esmodule一起使用,而const/request语法在commonjs中使用。

最新更新