serverless-webpack: Error when packager is yarn
This is a Bug Report
Description
When using nodeExternals in webpack config, deployment throws an error. The issue is only reproduced with includeModules: true + packager: 'yarn'.
Error --------------------------------------------------
Command failed: yarn install --frozen-lockfile --non-interactive
error An unexpected error occurred: "Your lockfile needs to be updated, but yarn was run with `--frozen-lockfile`.".
It may help because the deployment only include production packages, which causes a difference in yarn lockfile.
Additional Data
- Serverless-Webpack: 5.1.1
- Webpack version: 4.5.0
- Serverless Framework: 1.26.1
- webpack-node-externals: 1.7.2
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 17 (4 by maintainers)
I’m getting this same issue, but with no local modules and with a
yarn.lockfile that’s in sync withpackage.json. I tried deletingyarn.lockand runningyarn installto generate a new one, but it generated an identical file to the one I was already using.If I remove
nodeExternals()fromexternalsinwebpack.config.jsthen I no longer get the problem.FWIW i’ve also noticed this if I’m using a dependency that a module included but I didn’t explicitly add as a dependency. E.g. I have in one of my files:
it isn’t in my package.json, but I get this ok because it comes via
apollo-server-lambda->apollo-server-core->apollo-datasource. I useserverless offlineto run the code locally.On
serverless deploy, I see:but if i do
yarn add apollo-datasource, it deploys ok.I suspect it is good practice to have as package dependencies the modules you explicitly use in your package, e.g. you shouldn’t be able to import ‘lodash’ just because some module you use has it as a dependency. However, why
serverless deploydoesn’t warn or fail outright because the dependency isn’t explicitly added is a a puzzle to me.I was having the same problem ☝️
Something that I noticed, is that the
Packing external moduleswas outputtingbabel-runtime, without a version:all other modules have a version.
Adding
babel-runtimein mypackage.json“fixed” the problem.But adding
babel-runtimedidn’t feel right, so I ended removing all my dependencies, then added one-by-one back, to find out thatbabel-plugin-transform-object-entrieswas causing the problem, but since I no longer need it, I removed it and the problem was gone 🙏In my case, I’m not using local references. But while reading the output from serverless deployment, I just found out what was happening. In the list of modules in
Packing external modulesstage, there was a peer dependency trying to be packaged that was actually missing from my package.json. Then it was throwing the error.Regarding the discussion above, the
--frozen-lockfilecompletely makes sense to me.Thank you @mzmiric5 and @HyperBrain.
When you got error, open
.serverless/dependencies/package.jsonyou can see one of package has no version. EX:"aws-sdk: "".RESOLVED To resolve this, you just run
yarn add --dev aws-sdkor without--devfor some packages. Then run deploy again.I’m running into a similar issue, so I checked my
.webpacl/dependencies/package.jsonVS mypackage.jsonand there are some differences:Here is the package.json:
I don’t understand why
source-map-supportandtransform-runtimeare empty.Here is my
.babelrc:Any idea? I tried deleting/recreating the yarn.lock as well but didn’t fix anything.
–
Edit: We fixed it by using babel-runtime and source-map-support as dependencies, I don’t really understand why it works this way, it’s very confusing to say the least.
@bazaglia @mzmiric5 This is intended behavior. The plugin sets
--frozen-lockfileto prevent the build/deployment server install any new versions on its own and so change the module dependencies. You have to make sure that you commit a yarn.lock file in your projects that matches the package.json.You should check, if your yarn.lock file is up-to-date and matches everything that is in your package.json. If you change your package.json manually, they are out of sync and the packaging will fail.
As long as you do a
yarn installafter changing the files and useyarn addto add linked of local (file) packages, it will generate a matching yarn.lock file and should work.However, I can add an option like
custom: webpack: packagerOptions: noFrozenLockFile: truethat can be set in theserverless.yml. Imo it must default to freeze because of CI/CD stability but then you could disable the behavior easily for unstable testing environments.@mzmiric5 :
That might be a small different bug in the plugin because local references are rebased. I think the package lock file is not rebased properly in that case.
@clarsen Confirmed I just ran into the same issue. Mine was a difference in:
Where
"apollo-server-lambda": "2.0.5"is my actual package dependency. VS code auto-importedcoreand I didn’t even notice.The noFrozenLockfile option would be very useful for a project I am currently working on. Do you know when it will be released?