serverless-components / express

⚡ Take existing Express.js apps and host them easily on cheap, auto-scaling, serverless infrastructure (AWS Lambda and AWS HTTP API).
https://serverless.com/components
Apache License 2.0
375 stars 34 forks source link

Deployment fails to find module when upgrading from v1.7.5 to v2.0.3 #60

Closed Cameron-C-Chapman closed 3 years ago

Cameron-C-Chapman commented 3 years ago

I wasn't specifying a version for the component in the serverless.yml file and when I deployed, I pulled in the latest version of the component and ran into the following error:

Error: Cannot find module '..'
Require stack:
- /var/task/_express/handler.js
- /var/task/_serverless/handler.js
- /var/runtime/UserFunction.js
- /var/runtime/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/var/task/_express/handler.js:16:9)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)

When I compared the 2 deployment packages (the last known working version and the latest that failed), I noticed the version difference and was able to resolve the issue by rolling back to 1.7.5 by specifying the version in my component property in the serverless.yml file.

The deployment packages look the same at a glance, and my application was bundled/packaged the same in both cases.

The @serverless/platform-client version went from 1.1.6 to 3.8.0 which was interesting but it's hard to tell exactly what is different between the 2 deployments.

I'm happy to provide more information, I'm just not sure what would be useful, but I wanted to report this as an issue.

eahefnawy commented 3 years ago

Thanks @Cameron-C-Chapman ... does your package.json has a main: app.js property?

freddieerg commented 3 years ago

Thanks @Cameron-C-Chapman ... does your package.json has a main: app.js property?

Ok so judging by what you're saying the v2.0.3 update actually now checks your package.json for the main entry instead of what it did previously which was to force entry as app.js at the root directory, is this whats happened?

eahefnawy commented 3 years ago

@freddieerg yep that's right

Cameron-C-Chapman commented 3 years ago

Thanks @Cameron-C-Chapman ... does your package.json has a main: app.js property?

I do have the main property set to app.js in my package.json. I have a build process that builds the app and places it in a dist folder instead of the root folder.

Here is my input:src setting in the serverelss.yml file:

src:
    src: ./
    hook: npm run build
    dist: ./dist/

The dist folder output just contains an app.js and an app.js.map file.

image

Should I tweak the package.json or the dist configuration at all?

freddieerg commented 3 years ago

@freddieerg yep that's right

Thank you! This was a much needed change IMO! :)

rrooding commented 3 years ago

I'm using Nx.dev and so I only have one package.json at the top level of the project containing many apps and libraries. Any way I can fix this issue in another way?

EDIT: I fixed this by using the generate-package-json-webpack-plugin to generate a single specific package.json in the dist directory during the build.

albertocevallos commented 3 years ago

@Cameron-C-Chapman how did you fix this? I'm trying to setup a new service using:

create --template-url https://github.com/serverless-components/express/tree/master/templates/express-starter-typescript-webpack --path service

I noticed the difference between the express-starter template and the express-starter-typescript-tsc and express-starter-typescript-webpack was the latter didn't have the main: app.js property in package.json @eahefnawy described.

I updated my package.json to have main: app.js but still getting the same error you're getting:

` Error: Cannot find module '..' Require stack:

Anyways, any help on this would be great!

albertocevallos commented 3 years ago

UPDATE

This fixed it:

https://github.com/serverless-components/express/issues/12#issuecomment-695839017

Cameron-C-Chapman commented 3 years ago

@albertocevallos I can't track down any change that would indicate this but for some reason the pre v2 version of this component seemed to honor a dist directory but not the post v2.

I also can't find any evidence that the dist configuration was ever explicitly accounted for, but the typescript template you are referring to has that set, and my current app that has the compiled app.js put in a dist folder and used that config worked before as well.

I have the main property set, it seems to just not be loading the app from the dist folder, but instead trying to load it from the src folder.

Again, this was working for me before with no changes on my end. My config is just like the comment you linked to, but using parcel instead of webpack.

I was thinking it might have to do with not looking in the configured dist folder and gave a fix a stab, #61, but maybe it has to do with node_modules no longer copying or being found like they were in a pre v2 version?

albertocevallos commented 3 years ago

@Cameron-C-Chapman I'd suggest trying webpack instead, unless there's a specific reason to use Parcel (cache?).

And then using https://github.com/serverless-components/express/issues/12#issuecomment-695839017 and replacing dist with build .

Cameron-C-Chapman commented 3 years ago

@albertocevallos I finally got back to this and got it to work using the suggestions in this and the various linked threads 🎉

Thanks for helping me through that 😄

I'll go ahead and close this issue.