Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not deploy local dependencies #325

Closed
wandergeek opened this issue Jul 21, 2021 · 5 comments
Closed

Can not deploy local dependencies #325

wandergeek opened this issue Jul 21, 2021 · 5 comments

Comments

@wandergeek
Copy link

Hey there!

I've been having a lot of fun with this toolkit so first off, shout out to such a great project! Second off, nodejs is not my strong suit, so if this is a total noob question, please feel free to tell me to RTFM and point me to any relevant documentation.

I've been trying to import a local module of helper functions into my functions and I seem to be having a hard time. This is what I've tried so far:

Created a new module

mkdir lib
cd lib
npm init
cd ..
npm install --save ./lib/

results in package.json in root

  "dependencies": {
    "lib": "file:lib",
    "twilio": "^3.56"
  },

Adding code to ../lib/a.js

module.exports = {
    blahFunc: async (bla) => {
    }
};

Use the module in my twilio function

var a = require('../lib/a');
a.blahFunc(bla);

This works totally fine when testing locally via npm start, but as soon as I deploy, I get this error message:

✖ Failed Deployment

│ ERROR Failed API Request 20001
│
│ Some dependencies could not be validated. No matching version found for mylib@file:mylib.
│
│ More info: https://www.twilio.com/docs/errors/20001

Where error 20001 points to an error called Unknown parameters.

Can one of you twilios out there lend a pal a hand?

Thanks heaps!

@welcome
Copy link

welcome bot commented Jul 21, 2021

Thank you so much for opening your first issue in this project! We'll try to get back to it as quickly as possible. While you are waiting...here's a random picture of a corgi (powered by dog.ceo)

picture of dog

@philnash
Copy link
Contributor

Hey @wandergeek, delighted to hear you're enjoying working with the toolkit!

The issue that you have here is based on how the toolkit chooses files to upload and interacts with the Runtime API. You've added a dependency to a local directory within the project, which certainly works in regular Node.js situations, such as running the project locally. The toolkit does not automatically upload files from the lib directory so when the project is built on Twilio's side, the platform sees a locally defined dependency yet cannot find the files for it, so it fails.

In this case, it's best to try to work with the way that the Twilio Functions platform wants you to work. In your Functions that live in the functions directory you can include other Functions or JavaScript Assets. You can also make those Functions or Assets private, so that they can't be accessed via a URL. So, in your example you could do this instead:

Create ./assets/a.private.js with this content:

module.exports = {
    blahFunc: async (bla) => {
        // blahFunc implementation
    }
};

Then in your Function you need to use the function Runtime.getAssets() to get the path of the file within Twilio Runtime and then require it like this:

const a = require(Runtime.getAssets()["/a.js"].path);
a.blahFunc(bla)

You can see an example of how I've done this in this example Jira plugin using Twilio Functions (see the utils.private.js in assets which is used by most of the functions).

The Toolkit currently supports the way that the Twilio platform expects things to work, but it also works as if it is a regular Node.js project on your machine, which is why your project worked locally. We have some ideas for detecting things that will work locally but won't when deployed but we haven't managed to implement them just yet.

Let me know if this explanation helps and if you are able to deploy your project successfully.

@wandergeek
Copy link
Author

Oh man, that's perfect-- that worked like a charm. Thanks so much for taking the time explain this with examples, and once again thanks for the great work!

@wandergeek
Copy link
Author

I also just noticed you're also from Melbourne! Hello from Brunswick! :D

@philnash
Copy link
Contributor

Glad that worked out for you! And hello from Fitzroy 👋 not far away at all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants