在 Azure Typescript 函数中使用 ES 模块包
Using ES Module packages in Azure Typescript Function
我无法在我的 Azure 函数中使用包 p-map。我收到以下错误:
Worker failed to load function: 'serverless' with function id: '<id>'.
Result: Failure
Exception: Worker was unable to load function serverless: 'Error [ERR_REQUIRE_ESM]: require() of ES Module <es-module> from /usr/local/Cellar/azure-functions-core-tools@4/4.0.4483/workers/node/worker-bundle.js not supported.
Instead change the require of index.js in /usr/local/Cellar/azure-functions-core-tools@4/4.0.4483/workers/node/worker-bundle.js to a dynamic import() which is available in all CommonJS modules.'
项目是通过关注the steps in Azure's documentation创建的。以下是在index.ts:
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import pMap from "p-map";
import got from "got";
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
const sites = [
'https://avajs.dev',
'https://github.com'
];
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await pMap(sites, mapper, {concurrency: 2});
context.res = {
// status: 200, /* Defaults to 200 */
body: result
};
};
export default httpTrigger;
我的 tsconfig.json 如下所示:
{
"compilerOptions": {
"module": "es2020",
"target": "es2020",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": false,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
}
最后,这是我的 package.json:
{
"name": "azure-functions-test",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"prestart": "npm run build",
"start": "func start",
"test": "echo \"No tests yet...\""
},
"dependencies": {
"got": "^12.0.4",
"p-map": "^5.3.0"
},
"devDependencies": {
"@azure/functions": "^3.0.0",
"typescript": "^4.0.0"
}
}
p-map is strictly an ES Module and cannot be used in CommonJS projects.
我是不是遗漏了什么,或者只是无法在 Azure Functions 中使用 ES 模块包?提前致谢。
GitHub 上述代码的存储库,用于在本地进行测试:azure-functions-test
我通过将 index.ts 文件重命名为 index.mts 解决了我的问题。这在我的 dist 文件夹中构建了一个 index.mjs 文件(在 运行 npm run build
之后),它解决了这个问题。
需要注意的一件事是,您还必须编辑 function.json 的 scriptFile 密钥,以便它使用您的 .mjs 文件而不是 non-existing .js 文件。
我无法在我的 Azure 函数中使用包 p-map。我收到以下错误:
Worker failed to load function: 'serverless' with function id: '<id>'.
Result: Failure
Exception: Worker was unable to load function serverless: 'Error [ERR_REQUIRE_ESM]: require() of ES Module <es-module> from /usr/local/Cellar/azure-functions-core-tools@4/4.0.4483/workers/node/worker-bundle.js not supported.
Instead change the require of index.js in /usr/local/Cellar/azure-functions-core-tools@4/4.0.4483/workers/node/worker-bundle.js to a dynamic import() which is available in all CommonJS modules.'
项目是通过关注the steps in Azure's documentation创建的。以下是在index.ts:
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import pMap from "p-map";
import got from "got";
const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
const sites = [
'https://avajs.dev',
'https://github.com'
];
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await pMap(sites, mapper, {concurrency: 2});
context.res = {
// status: 200, /* Defaults to 200 */
body: result
};
};
export default httpTrigger;
我的 tsconfig.json 如下所示:
{
"compilerOptions": {
"module": "es2020",
"target": "es2020",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": false,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
}
最后,这是我的 package.json:
{
"name": "azure-functions-test",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"prestart": "npm run build",
"start": "func start",
"test": "echo \"No tests yet...\""
},
"dependencies": {
"got": "^12.0.4",
"p-map": "^5.3.0"
},
"devDependencies": {
"@azure/functions": "^3.0.0",
"typescript": "^4.0.0"
}
}
p-map is strictly an ES Module and cannot be used in CommonJS projects.
我是不是遗漏了什么,或者只是无法在 Azure Functions 中使用 ES 模块包?提前致谢。
GitHub 上述代码的存储库,用于在本地进行测试:azure-functions-test
我通过将 index.ts 文件重命名为 index.mts 解决了我的问题。这在我的 dist 文件夹中构建了一个 index.mjs 文件(在 运行 npm run build
之后),它解决了这个问题。
需要注意的一件事是,您还必须编辑 function.json 的 scriptFile 密钥,以便它使用您的 .mjs 文件而不是 non-existing .js 文件。