【发布时间】:2018-05-11 18:35:56
【问题描述】:
我正在为 Google 云功能编写代码。这里我想使用包括URLSearchParams 在内的URL 标准。我发现它们是 TypeScript DOM 库的一部分,所以我已将其添加到我的 tsconfig lib 设置中。
但是,当我编译和部署云函数时,我收到一个运行时错误,提示 URLSearchParams 未定义。
我错过了什么?我正在使用 TS 2.6
这是我的配置:
{
"compilerOptions": {
/* Basic Options */
"target": "es6",
"module": "commonjs",
"lib": ["es6", "es7", "esnext", "dom"],
"sourceMap": true /* Generates corresponding '.map' file. */,
"outDir": "build" /* Redirect output structure to the directory. */,
"removeComments": true /* Do not emit comments to output. */,
/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"plugins": [{ "name": "tslint-language-service" }],
"skipLibCheck": false
},
"include": ["src/**/*"],
"exclude": ["build"]
}
以及 package.json 依赖:
"dependencies": {
"circular-json": "^0.4.0",
"es6-promisify": "^5.0.0",
"firebase-admin": "^5.5.0",
"firebase-functions": "^0.7.3",
"invariant": "^2.2.2",
"joi": "12",
"lodash": "^4.17.4",
"node-fetch": "^2.0.0-alpha.9"
},
"devDependencies": {
"@types/circular-json": "^0.4.0",
"@types/invariant": "^2.2.29",
"@types/joi": "^13.0.0",
"@types/lodash": "^4.14.85",
"@types/node": "^8.0.52",
"@types/node-fetch": "^1.6.7",
"cpy-cli": "^1.0.1",
"del-cli": "^1.1.0",
"firebase-tools": "^3.15.1",
"tslint": "^5.8.0",
"tslint-config-prettier": "^1.6.0",
"tslint-language-service": "^0.9.6",
"typescript": "2.6"
}
【问题讨论】:
-
您能分享您的(缩写)package.config 和 tsconfig.json 以便我们尝试重现吗?
-
如果一个函数在开发期间存在(它在你的 .d.ts 文件或 tsconfig 文件中)但在运行时失败,这通常意味着你的实际网站(html 页面)没有加载了所需的库或您的浏览器不支持您使用的功能。
-
@Kokodoko 我没有使用浏览器。问题是如何在服务器上使用此 API
-
@Fenton 我已经添加了它们
-
@Kokodoko 澄清一下,较新版本的 Node 已经实现了这个 API,但是由于 Google Cloud Functions 使用 Node 6,我正在尝试使用 Typescript 实现。
标签: typescript google-cloud-functions typescript-lib-dom