To use JavaScript / ECMAScript modules (ES modules, since ES6) with iisnode you have to create a new entry point file.
server.cjs
import('./app.mjs');
Now just set the server.cjs instead of app.js in the web.config.
<configuration><system.webServer><iisnodelogDirectory="c:\inetpub\wwwroot\my-website\_data\iisnode"devErrorsEnabled="false"flushResponse="true"enableXFF="true" /><httpErrors existingResponse="PassThrough" /><handlers><add name="iisnode" path="server.cjs" verb="*" modules="iisnode" /></handlers><rewrite><rules><rule name="NodeApp"><match url=".*" /><action type="Rewrite" url="server.cjs" /></rule></rules></rewrite></system.webServer></configuration>
That's it! Your node app will now use the ES modules system.
What is the .cjs and .mjs file extension?
- .cjs - uses the node's origin CommonJS module system
- .mjs - uses ES module
- .js - by default same as .cjs
In the package.json we can change the default module system to ES modules.
.js file extensions will then be treated the same as .mjs files.
// package.json{"type": "module", // default value is "commonjs""name": "my-app",...}