JS SDK ECMAScript import issue

Hi,

I trying to make Moralis work in AWS Lambda using NodeJs16.
I use the ECMAScript syntax and it doesn’t work for me.

I followed the documentation:

import Moralis from 'moralis';
import { EvmChain } from '@moralisweb3/evm-utils';

I get the following error

{
    "errorType": "Runtime.UnhandledPromiseRejection",
    "errorMessage": "SyntaxError: Cannot use import statement outside a module",
    "reason": {
        "errorType": "SyntaxError",
        "errorMessage": "Cannot use import statement outside a module",
        "stack": [
            "/var/task/node_modules/moralis/lib.esm/index.js:1",
            "import { Streams } from '@moralisweb3/streams';",
            "^^^^^^",
            "",
            "SyntaxError: Cannot use import statement outside a module",
            "    at Object.compileFunction (node:vm:352:18)",
            "    at wrapSafe (node:internal/modules/cjs/loader:1033:15)",
            "    at Module._compile (node:internal/modules/cjs/loader:1069:27)",
            "    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)",
            "    at Module.load (node:internal/modules/cjs/loader:981:32)",
            "    at Function.Module._load (node:internal/modules/cjs/loader:822:12)",
            "    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:163:29)",
            "    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)",
            "    at async Promise.all (index 0)",
            "    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)"
        ]
    },
    "promise": {},
    "stack": [
        "Runtime.UnhandledPromiseRejection: SyntaxError: Cannot use import statement outside a module",
        "    at process.<anonymous> (file:///var/runtime/index.mjs:1194:17)",
        "    at process.emit (node:events:527:28)",
        "    at emit (node:internal/process/promises:140:20)",
        "    at processPromiseRejections (node:internal/process/promises:274:27)",
        "    at processTicksAndRejections (node:internal/process/task_queues:97:32)"
    ]
}

My package.json is as follow:

{
    "name": "NFT Web gallery",
    "description": "Dependencies for the Lambdas deployed by SAM",
    "version": "0.0.1",
    "private": true,
    "type": "module",
    "dependencies": {
        "@moralisweb3/evm-utils": "^2.14.1",
        "ethers": "^6.0.2",
        "moralis": "^2.14.1",
        "node-fetch": "^3.3.0"
    },
    "devDependencies": {
        "jest": "^26.6.3"
    },
    "scripts": {
        "test": "jest"
    }
}

I used to have to hack around to declare it using import. After I saw that the documentation has been updated to use import I thought it would work easy but no.

Any help would be appreciated.

Best

there are different ways to import moralis sdk depending on the context, maybe this forum post helps

I already wrap everything in an async function. Also I was testing to use import and not require.

Note that on AWS Lambda, using the ECMAScript syntax is more performant and reduce your execution time and $.

The following AWS tutorial still uses the common syntax require which doesn’t match the Moralis documentation anymore:

I had to revert back to using require because it just doesn’t work with import. The problem is within @moralisweb3/streams as per the error message in my post. I am not importing this specific streams module.

Hey @koxon,

try to set the "type": "module" in your package.json if you like to work on import but thank you for your input here

I already use module, read my post above.

you could try the options from this thread:

I don’t know if it helps

can you try with Moralis SDK version 2.10.0 or 2.13.0?

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.