node.jsibm-cloudibm-cloud-code-engine

IBM Cloud Code Engine with Node.js, Issues Importing Code Engine SDK


I'm trying to create a Node.js Job that uses the Code Engine SDK so that I can trigger other jobs as necessary.

The problem I'm having is that following the IBM documentation I don't seem to be able to get the SDK to import.

I have installed two SDKs for IBM Cloud:

npm install ibm-cloud-sdk-core
npm install @ibm-cloud/ibm-code-engine-sdk

The install commands work ok and I can see it in node_modules/.

I can import the IamAuthenticator module from the core SDK without any issues:

import { IamAuthenticator } from 'ibm-cloud-sdk-core';

But when I try to import CodeEngineV2 from the Code Engine SDK using any of the below methods I get errors.

The IBM Documentation uses the below command:

import CodeEngineV2 from "@ibm-cloud/ibm-code-engine-sdk/dist/code-engine/v2";

which returns this error:

Uncaught Error Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Source\Projects\code-engine-test-repo\node_modules\@ibm-cloud\ibm-code-engine-sdk\dist\code-engine\v2' imported from C:\Source\Projects\code-engine-test-repo\index.js
Did you mean to import @ibm-cloud/ibm-code-engine-sdk/dist/code-engine/v2.js?

If I update the import reference to v2.js then I get this error;

(node:20804) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

Adding "type": "module" to my package.json then just returns this:

Uncaught ReferenceError ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'C:\Source\Projects\code-engine-test-repo\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

index.js

import { IamAuthenticator } from 'ibm-cloud-sdk-core';
import { CodeEngineV2 } from '@ibm-cloud/ibm-code-engine-sdk/dist/code-engine/v2';

async function main() {
  try {
    // Load secrets from Code Engine
    const iamAuthenticator = new IamAuthenticator({ apikey: process.env.CODE_ENGINE_API_KEY });
    console.log(`Batch job completed successfully`);
  } catch (error) {
    console.error('Error processing batch job:', error);
  }  
}

main();

package.json

{
  "name": "code-engine-test-repo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js",
    "pretty": "prettier --write \"./**/*.{js,jsx,json}\""
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "node-env-run": "^4.0.2"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "dependencies": {
    "@ibm-cloud/ibm-code-engine-sdk": "^3.1.0",
    "ibm-cloud-sdk-core": "^4.0.5"
  }
}


Solution

  • I realised my mistake, I had used a named import when I shouldn't have.

    import CodeEngineV2 from '@ibm-cloud/ibm-code-engine-sdk/dist/code-engine/v2.js';
    

    Thanks to jonrsharpe who nudged me in the right direction