I followed the instructions to import them from here:https://docs.mongodb.com/realm/functions/import-external-dependencies/
For some reason, the .tar file did not work so I had to use .zip.
The modules show up on the UI, however importing them yields the error Cannot find module 'module-name'.
I have tried both the UI upload and through realm-cli.
I am having the same issue as the original poster.
I create a .zip file, uploaded it manually, and the modules appear in the UI.
When I try to require it I get a ‘not found’ error. Please help.
I am not able to upload a screenshot (permission denied), but yes, the modules exist in the UI, yes the app is deployed.
My code is simply:
exports = async function getTimezones() {
const moment = require("moment");
console.log('moment found???');
};
with the error:
Cannot find module ‘moment’
trace:
FunctionError: Cannot find module ‘moment’
at require ()
at getTimezones$ (function.js:6:18)
at call ()
at tryCatch (:55:37)
at invoke (:281:22)
at :107:16
at call ()
at tryCatch (:55:37)
at invoke (:145:20)
at :180:11
at :3:6
at callInvokeWithMethodAndArg ()
at enqueue (:202:13)
at :107:16
at :226:9
at getTimezones (function.js:4:11)
at apply ()
at function_wrapper.js:3:1
at :11:1
Update: I followed the exactly how Andrew wrote them, and that seemed to work. This was beyond frustrating – I have a full page of notes I tried that didn’t work.
For future people, the difference between this “working” and not is either:
something internal on their side with the tar.gz
installing packages with npm (rather than zipping them manually) includes a handful of @ folders (ex. @babel) that might need to be included. I did an npm install --only=prod version.
Thanks for letting us know, Eve. Sorry to hear that you had a difficult experience getting function dependencies working. I work on the Realm docs team, and I would be happy to update the existing import docs if you have any other information in your notes. I’d definitely like to keep other users from experiencing this level of frustration in the future!
In particular, I’m interested in hearing more about what the “something internal” that went wrong with your tar.gz was, and why --only=prod seemed to make a difference for you.
> ran on Fri Mar 19 2021 01:52:44 GMT+0300 (GMT+03:00)
> took 261.423531ms
> error:
failed to eval source for module 'nanoid': node_modules/nanoid/index.cjs: Line 9:7 Unexpected identifier (and 16 more errors)
> trace:
FunctionError: failed to eval source for module 'nanoid': node_modules/nanoid/index.cjs: Line 9:7 Unexpected identifier (and 16 more errors)
at require (<native code>)
at exports (function.js:2:24)
at apply (<native code>)
at function_wrapper.js:2:13
at <anonymous>:11:1
I have a weirder issue.
When I run the function individually, either from a client SDK or the web its works and the dependency is being imported and used.
When the function runs as part of a Trigger (Auth trigger for example) the dependency is not found and function fails.
From the logs:
Cannot find module ‘myModule’
Note:
The app is released to the Store and was working fine for the past 2-3 months. Something happened this month and it stopped working on with Triggers.
@Andrew_Morgan I have also tried something I thought maybe could overcome the issue until the problem is known.
Since by calling the function explicitly without a trigger it works, I thought I would replace the Trigger function with a proxy function that would in turn call my original function.
@Nemanja_Trivic so that was the initial problem the proxy is trying to solve. Not having the import in the Trigger. Anyhow, I have just tried it. It still not work.
@Andrew_Morgan mmm that is weird. The thing is that I have done the exact steps. And it’s not that it is not working generally. It is, it’s only failing on a Trigger (Auth, don’t know about Database)
Can you think of any reason why it wouldn’t?
–
From my side. I have provided a quick fix around this issue for now. and I will try to create a fresh App next week to try it like you did – just to cover my bases.
@Bob_Dylan@Georges_Jamous I just called my same trigger from a login trigger — and sure enough, I get the dependency error when it runs. I’ll investigate some more.
In the meantime, one workaround would be to have your auth trigger insert a document into a “notifications” collection. There would be a database “insert” trigger on the “notifications” collection which did the actual work (including the use of your dependency) and optionally delete the notification document.