0

I know it is a bad practice to use the import as well as require statements in the same file, but I heard it shouldn't cause any problems.
Why will my lambda fail then (when running yarn run local) and complain about an 'Unexpected Identifier' when encountering the import statement?
Here's the current codebase. The problem lies in the functions/edge.js file.

EDIT: I'm sorry I haven't clearly formulated my question. Replacing the import statement with the seemingly equivalent const middleware = require('@sapper/server'); results in an error: It can't find the module - with import it works perfectly fine, even during production.

1 Answers1

1

Because AWS Lambdas run on node, and the version of node AWS Lambda use don't support import keyword.

More info on NodeJS plans to support import/export es6 (es2015) modules

EDIT: As @Michael states in the comments, you need to install the proper packages. Either by using npm or looking where the package should be (I guess you should follow sapper.svelte instructions properly). import would fail the same way as require as the package don't exists. Is not an "import vs require" problem, but a non-existent package problem.

Jorge Fuentes González
  • 10,682
  • 4
  • 38
  • 58
  • How can I do the same import I'm doing in `functions/edge.js` with require() instead? – SearchingSolutions Aug 12 '19 at 18:37
  • 1
    The same way you do all the other imports in that file - use `const middleware = require("modulename")` – Michael Aug 12 '19 at 19:12
  • @SearchingSolutions as Michael says, `require('@sapper/server');` in this case. An import is nothing more than a fancy require. – Jorge Fuentes González Aug 12 '19 at 19:37
  • @Michael That's the problem: While the import statement would work perfectly fine, `require ('@sapper/server')` instead of importing it throws an error - it somehow can't find the module. Are you sure that's how you import namespaces with require? – SearchingSolutions Aug 12 '19 at 19:56
  • 1
    Positive - it looks like you're missing the `@sapper/server` from your package JSON dependencies section which will be causing it to not be found. `npm i --save @sapper/server` – Michael Aug 13 '19 at 06:03
  • @Michael I updated the import statement and changed it into an equivalent require statement. When running `yarn run local` (Serverless local invocation) it can't find the module. I can't require `sapper` directly. How can I import `@sapper/server` like with an import (see `src/server.js` for the original implementation)? – SearchingSolutions Aug 14 '19 at 18:53
  • @SearchingSolutions I've edited my answer. `@sapper` seems part of `sveltle`. You must follow their indications so you can have `@sapper` installed. – Jorge Fuentes González Aug 15 '19 at 14:30