【问题标题】:Azure App Service - Nodejs ES Module ErrorAzure 应用服务 - Nodejs ES 模块错误
【发布时间】:2021-04-18 13:17:35
【问题描述】:

尝试在 azure 应用服务上托管一个简单的 nodejs api 服务器,但是当 azure 尝试部署它时出现以下错误

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\home\site\wwwroot\server.js
require() of ES modules is not supported.
require() of D:\home\site\wwwroot\server.js from D:\Program Files (x86)\iisnode\interceptor.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename server.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\home\site\wwwroot\package.json.

检查了azure app服务,WEBSITE_NODE_DEFAULT_VERSION设置为~14,web app中安装的nodejs版本为v14.15.0。不要再认为这个版本的node有导入导出的问题了。

代码在本地运行得非常好

node server.js 
# or
node --experimental-modules server

不知道为什么它在 azure 中失败

我的代码如下:

server.js

import express from 'express';
import bodyParser from 'body-parser';

let app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

let port = process.env.PORT || 8080;
let router = express.Router(); 

// Middleware to use for all requests
router.use(function(req, res, next) {
    next();
});

router.get('/', function(req, res) {
    res.json({ message: 'Default home page for the api!' });
});

app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);

console.log(`Server up and running on port ${port}`);

package.json

{
    "name": "my_package",
    "version": "1.0.0",
    "type": "module",
    "scripts": {
      "start": "node --experimental-modules server",
      "test": "mocha"
    },
    "dependencies": {
      "body-parser": "^1.19.0",
      "express": "^4.17.1"
    }
  }
  

但是,如果我将导入和导出更改为要求,那么它将在 azure web 应用程序中运行,似乎 azure iisnode 可能与 emac6 不兼容?有谁知道吗?

除了使用 babel 将 emac6 转换为 emac5 之外,任何人都可以解决这个问题?因为我在执行和运行转译的 emac5 代码时遇到了一些问题。

谢谢

【问题讨论】:

    标签: javascript node.js azure


    【解决方案1】:

    在package.json 文件中添加"type": "module"。它对我有用。

    {
      "name": "module-error",
      "version": "1.0.0",
      "description": "",
      "main": "server.js",
      "scripts": {
        "start": "node --experimental-modules server",
        "test": "mocha"
      },
      "dependencies": {
        "body-parser": "^1.19.0",
        "express": "^4.17.1"
      },
      "author": "",
      "license": "ISC",
      "type": "module"
    }
    

    【讨论】:

    • @Ohhh 你能在没有敏感信息的情况下显示你的示例代码吗?因为我按照你的描述,无法重现你提到的问题。
    • 一切都在帖子中,你能打开并导航到网址吗?
    • @Ohhh 你的问题解决了吗?有进展吗?
    • 不幸的是没有lollz,我将代码从ES6转译到ES5,发现它仍然缺少一些我需要的程序,所以我从App Service改为azure VM而不是lolzz。但感谢您的帮助:) 赞赏
    【解决方案2】:

    这可以通过在您的 server.js 旁边添加一个新文件并将其配置为应用服务(或更具体地 iisnode)的入口点来解决(请参阅您的 web.config)。

    让我们将新文件命名为run.cjs,并在其中只放入以下行:

    import("./server.js");
    

    cjs 文件扩展名很重要,因为它告诉 Node 该文件不是 ES 模块,正如它所期望的那样,因为 package.json 中的 "type": "module"。这允许其他 CommonJS 文件包含我们的新文件 - 即 iisnode 的interceptor.js。 它再次导入server.js,然后作为 ES 模块正常运行。

    【讨论】:

      【解决方案3】:

      补充@sibbl 所说的内容

      1. 只需打开您的 web.config
      2. 搜索server.js 替换为run.cjs

      有效! 再次感谢@sibbl

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-10
        • 2019-03-24
        • 2023-02-14
        • 2021-12-05
        • 1970-01-01
        • 2021-10-31
        • 2018-01-12
        • 2018-12-05
        相关资源
        最近更新 更多