I am unable to use the package p-map in my Azure Function. I get the following error:
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 project is created by following the steps in Azure's documentation. The following is in the 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;
My tsconfig.json looks like following:
{
"compilerOptions": {
"module": "es2020",
"target": "es2020",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": false,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
}
}
Lastly, this is my 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.
Am I missing something or is it just not possible to use ES Module packages in Azure Functions? Thanks in advance.
GitHub repository of aforementioned code to test things out locally: azure-functions-test
I resolved my issue by renaming my index.ts file to index.mts. This built a index.mjs file (after running npm run build
) in my dist folder which fixed the issue.
One thing to note is that you also have to edit your function.json's scriptFile key so it uses your .mjs file instead of non-existing .js file.