Welcome to the new Golem Cloud Docs! 👋
Documentation
Experimental Languages
JavaScript
Setting up the Golem JS SDK

Setting up the Golem JavaScript SDK

⚠️

If the project was created with golem-cli new, it already has the Golem TypeScript SDK set up and these steps can be ignored.

The Golem JavaScript SDK is the same as the Golem TypeScript SDK. The published SDK is usable both from JavaScript and TypeScript, as it is bundled as JavaScript with TypeScript type information.

If setting up the Golem JavaScript SDK (opens in a new tab) manually, the following steps are required:

Add the Golem JavaScript SDK and related tooling as a dependency

The Golem JavaScript SDK currently relies on forked versions of jco and componentize-js. Our examples contains the latest dependencies that work together, but in case you want setup you project manually, use the following samples as a starting point.

Example package.json:

{
  "scripts": {
    "build": "rollup --config",
    "componentize": "npm run build && jco componentize -w wit -o out/component_name.wasm out/main.js",
    "clean": "rm -rf out src/generated"
  },
  "devDependencies": {
    "@golemcloud/componentize-js": "0.10.2-golem.1",
    "@golemcloud/golem-ts": "0.2.0",
    "@golemcloud/jco": "1.4.0-golem.1",
    "@rollup/plugin-node-resolve": "^15.2.3",
    "rollup": "^4.18.0"
  }
}

Example rollup.config.mjs:

import resolve from "@rollup/plugin-node-resolve";
 
export default {
    input: 'src/main.js',
    output: {
    file: 'out/main.js',
    format: 'esm'
},
    external: ["golem:api/host@0.2.0"],
    plugins: [resolve()],
};

Add all the supported WIT files into the project

The project's wit/deps directory must contain all the WIT files from the golem-wit (opens in a new tab) repository.

Importing WITs into the component's world:

If the project's WIT file was like this:

package golem:demo;
 
world ts-example {
  export api;
}

Modify it in the following way:

package golem:demo;
 
world ts-example {
  import golem:api/host@0.2.0;
  import golem:rpc/types@0.1.0;
 
  import wasi:blobstore/blobstore;
  import wasi:blobstore/container;
  import wasi:cli/environment@0.2.0;
  import wasi:clocks/wall-clock@0.2.0;
  import wasi:clocks/monotonic-clock@0.2.0;
  import wasi:filesystem/preopens@0.2.0;
  import wasi:filesystem/types@0.2.0;
  import wasi:http/types@0.2.0;
  import wasi:http/outgoing-handler@0.2.0;
  import wasi:io/error@0.2.0;
  import wasi:io/poll@0.2.0;
  import wasi:io/streams@0.2.0;
  import wasi:keyvalue/eventual-batch@0.1.0;
  import wasi:keyvalue/eventual@0.1.0;
  import wasi:logging/logging;
  import wasi:random/random@0.2.0;
  import wasi:random/insecure@0.2.0;
  import wasi:random/insecure-seed@0.2.0;
  import wasi:sockets/ip-name-lookup@0.2.0;
  import wasi:sockets/instance-network@0.2.0;
 
  export api;
}