Azure部署Node.js应用遇ERR_REQUIRE_ESM错误,配置后仍异常
解决Azure Windows App Service部署ES模块Node.js应用的ERR_REQUIRE_ESM问题
问题概述
采用ES模块语法的Node.js应用部署到Azure Windows App Service后,访问端点触发Error [ERR_REQUIRE_ESM]——iisnode的interceptor.js尝试用require()加载ES模块index.js,不符合ESM规范。在package.json中添加"type": "module"后,又出现index.js被当作文本文件直接渲染、未执行的问题。
报错信息
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\home\site\wwwroot\index.js from C:\Program Files (x86)\iisnode\interceptor.js not supported. Instead change the require of index.js in C:\Program Files (x86)\iisnode\interceptor.js to a dynamic import() which is available in all CommonJS modules. at Module.patchedRequire [as require] (C:\PROGRA~2\SITEEX~1\NODEAP~1\296456~1.8\nodejs\node_modules\diagnostic-channel\dist\src\patchRequire.js:16:46) at Object.<anonymous> (C:\Program Files (x86)\iisnode\interceptor.js:459:1) Application has thrown an uncaught exception and is terminated: Error [ERR_REQUIRE_ESM]: require() of ES Module C:\home\site\wwwroot\index.js from C:\Program Files (x86)\iisnode\interceptor.js not supported. Instead change the require of index.js in C:\Program Files (x86)\iisnode\interceptor.js to a dynamic import() which is available in all CommonJS modules. at Module.patchedRequire [as require] (C:\PROGRA~2\SITEEX~1\NODEAP~1\296456~1.8\nodejs\node_modules\diagnostic-channel\dist\src\patchRequire.js:16:46) at Object.<anonymous> (C:\Program Files (x86)\iisnode\interceptor.js:459:1)
解决方案
1. 配置web.config绕过iisnode的require加载
在项目根目录创建web.config文件,配置iisnode直接调用Node.js命令执行入口文件,避免通过interceptor.js的require()加载ES模块:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="iisnode" path="index.js" verb="*" modules="iisnode" /> </handlers> <iisnode nodeProcessCommandLine=""%programfiles%\nodejs\18.17.0\node.exe"" processCommand="node "%home%\site\wwwroot\index.js"" promoteServerVars="HTTP_ACCEPT_ENCODING,HTTP_HOST" /> <rewrite> <rules> <!-- 重定向所有非文件请求到index.js --> <rule name="Node.js Routing" patternSyntax="ECMAScript"> <match url=".*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> </conditions> <action type="Rewrite" url="index.js" /> </rule> <!-- 禁止将index.js作为静态文件返回 --> <rule name="Block Static Index" stopProcessing="true"> <match url="index.js" /> <action type="None" /> </rule> </rules> </rewrite> <!-- 保护node_modules目录不被直接访问 --> <security> <requestFiltering> <hiddenSegments> <add segment="node_modules" /> </hiddenSegments> </requestFiltering> </security> </system.webServer> </configuration>
- 替换
nodeProcessCommandLine中的版本号为你应用使用的Node.js版本(可在Azure门户的应用服务设置中查看)。 processCommand指定直接用node命令执行入口文件,绕过iisnode的默认加载逻辑。
2. 确认package.json配置正确
确保package.json中已开启ES模块支持,且启动脚本正确:
{ "main": "index.js", "type": "module", "scripts": { "start": "node index.js" } }
3. 修复应用代码中的ES模块语法问题
检查应用代码,确保所有模块导入使用import语法,且依赖的模块均正确导入。比如你的代码中使用了path.resolve但未导入path模块,需添加:
import path from 'path';
原理说明
Azure Windows App Service默认通过iisnode的interceptor.js用require()加载Node.js入口文件,而ES模块不支持被CommonJS的require()加载。通过web.config配置processCommand直接调用node命令执行入口文件,让Node.js以ESM模式加载应用,同时通过rewrite规则确保所有请求转发到应用,避免入口文件被当作静态资源渲染。
内容的提问来源于stack exchange,提问作者markzzz
相关产品推荐
相关产品推荐

