apex / up

Deploy infinitely scalable serverless apps, apis, and sites in seconds to AWS.
https://up.docs.apex.sh
MIT License
8.8k stars 380 forks source link

Support ES Modules with the nodejs14.x runtime #827

Open SiegfriedEhret opened 3 years ago

SiegfriedEhret commented 3 years ago

Prerequisites

Description

I am using the Node.js 14.x runtime.

I can deploy a svelte kit app (with the node adapter) with up but accessing it fails because of the usage of ES Modules ("type": "module" in the package.json file).

Since Node 14 allows ECMAScript modules natively and without flag, it would be lovely to use them with up with the Node 14.x runtime.

Steps to Reproduce

To start, I init the svelte kit app:

npm init svelte@next my-app
cd my-app
npm install

Then, the node adapter:

npm i -D @sveltejs/adapter-node@next

Update the svelte.config.js: add the import and the adapter:

import adapter from '@sveltejs/adapter-node';

export default {
        ...
    kit: {
                ...
        adapter: adapter({
            // default options are shown
            out: 'build'
        })
    }
};

Then, add the up.json file:

{
    "name": "example",
    "profile": "example",
    "lambda": {
        "runtime": "nodejs14.x"
    },
    "regions": ["eu-central-1"],
    "stages": {
        "production": {
            "domain": "example.com"
        }
    }
}

Here is what I get when I open the domain in my browser:

{"message": "Internal server error"}

And in up logs:

  May 24th 05:10:50pm INFO 2021-05-24T15:10:50.444Z     undefined       ERROR   Uncaught Exception      {
  "errorType": "Error",
  "errorMessage": "Must use import to load ES Module: /var/task/_proxy.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/_proxy.js from /var/runtime/UserFunction.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.\nInstead rename _proxy.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.\n",
  "code": "ERR_REQUIRE_ESM",
  "stack": [
    "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/_proxy.js",
    "require() of ES modules is not supported.",
    "require() of /var/task/_proxy.js from /var/runtime/UserFunction.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 _proxy.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.",
    "",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)",
    "    at Module.load (internal/modules/cjs/loader.js:928:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:769:14)",
    "    at Module.require (internal/modules/cjs/loader.js:952:19)",
    "    at require (internal/modules/cjs/helpers.js:88:18)",
    "    at _tryRequire (/var/runtime/UserFunction.js:75:12)",
    "    at _loadUserApp (/var/runtime/UserFunction.js:95:12)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1063:30)"
  ]
}

Thanks!