Loading 3rd JS library

Hi,
i need to load a custom 3rd party JS package in order to implent a custom fetch (JS server method) for a specific type. How should i do this? Has anyone already do this?

Thank you :slight_smile:

In the package.json of the package in which you want to define this method, you can add a new js runtime like so:

{
  "name": "my-package",
  "description": "...",
  "author": "...",
  "runtimes": {
    "nodejs-myruntimename": {
      "language": "JavaScript",
      "modules": {
        // any js libraries you need here
        "request": "^2.79.0"
      }
    }
  }
}

you can then specify that runtime for any given method you’d like:

  @nodejs(typeSystem=true, env='myruntimename')
  myFunc: function() js server
function myFunc() {
  var req = require("request");
  ...
}

Note: when specifying a runtime in package.json the name of the runtime must follow the convention of nodejs-<name>. Then, the value of the env parameter in your method annotation should simply be <name>

Thank you sir.
Does it also work if the library is reperible from a custom git repository? Should i include in the package definition something like this?

“repositories”: [“https://github.com/joewalnes/filtrex”]

Thanks

In 7.9 the runtimes changed to a C3 Type which can be added via seed data. For instance if you were to want to use Python 3.6 you could create the following JSON file in the ActionRuntime seed folder:

{
    "id": "py-mmmhis",
    "name": "py-mmmhis",
    "language": "Python",
    "runtimeVersion": "3.6",
    "connector": "simple",
    "runtime": "CPython",
    "modules": {
        "conda.pyopenssl": "",
        "conda.requests": ""
    },
    "repositories": [
        "https://artifacts.c3-e.com/v1/anaconda"
    ]
}
1 Like

@FedericoA I am not sure if you can specify a custom git repository for this. My intuition is no, as c3ShowType(ActionRuntime) says this for the repositories field:

For Node.js, if repositories is defined, it must be an array of length one. The repository will be used by npm to find and install the modules .

If it is indeed possible, this would be done in the modules field, which is used for the dependencies part of a generated package.json.

npm allows git urls as dependencies, try one of these forms:
https://docs.npmjs.com/files/package.json#git-urls-as-dependencies