Javascript runtime call

Hi I would like to add a third party javascript library to my c3 package. Here is my configuration

{
“name”: “myPackage”,
“description”: “Hello world”,
“author”: “The c3 users”,
“dependencies”: [“xxx”],
“runtime”: {
“js-sha256”: {
“language”: “JS”,
“runtime”: “NODE_JS”,
“repositories”: [“https://github.com/emn178/js-sha256”]
}
}
}

here “js-sha256” is the third party javascript package. A testing call is sha256(‘11111’). However, console returns “sha256 is not defined”.
I know the language is “JS”, and runtime is “NODE_JS”, but I am not sure what is “repositories”. Can someone give an example how to call javascript runtime?

1 Like

Your runtime definition is off, please use something along those lines:

{
  "name": "myPackage",
  "runtimes": {
    "nodejs-hash": {
      "language": "JavaScript",
      "runtime": "Node.js",
      "modules": {
        "jssha": "^2.3.1"
      }
    }
  }
}

then to use it:

type MyType {
  @nodejs(env="hash")
  hashSomethingToSha256: function(text: !string) : !string
}

Hi, I have done the same as above to use a mailjet module in my function. However, in order to use the module I must first require it using var myVar = require(‘node-mailjet’).

When debugging the function that uses this module, it doesn’t recognize the require() call;
Uncaught ReferenceError: require is not defined

I tried adding require to the runtime definition but this didn’t help. Any ideas on how to solve this?

1 Like

Can you post your runtime definition as well as the function declaration from the C3 type file?
Also, what version of the platform are you using?

Runtime definition in package.json:

{
  "name": "myPackage",
  "runtimes": {
    "nodejs-mail": {
      "language": "JavaScript",
      "runtime": "Node.js",
      "modules": {
        "node-mailjet": "^3.3.1",
        "require": "^2.4.20"
      }
    }
  }
}

Function decleration:
@nodejs(env=“mail”)
emailTesting: function(): boolean js server

Platform version:
c3-server-7.8.8.76-1.x86_64.rpm

In your C3 type file, you’re declaring the function as both nodejs and js, so when you call the function, the platform is trying to run it in the js runtime. You want this function to be run in the nodejs runtime only, so remove js server from the function definition.

1 Like

Also you don’t need to add require to your list of modules in the nodejs-mail runtime—that is already built into nodejs.

Thanks Matt, I’ve made those changes but receiving a new error message:

Error executing command: node index.js↵/tmp/myType-emailTesting-3589644663043996189/index.js:36↵var logger = C3.logger(‘myType’);

ReferenceError: C3 is not defined at Object

Check out the documentation for the @nodejs annotation (c3ShowType(Ann.Nodejs)). By default, the C3 type system is not loaded, but if you need it, it can be loaded by providing true for the typeSystem property on the nodejs annotation. C3 should be defined after loading the type system.

1 Like