Bots with imports of custom code
# support
c
Do you have to do something special with bots to have shared modules that you import into your bots? I am getting errors that look like this:
Copy code
ERROR    Uncaught Exception     {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module './shared/caregiverAccessUtils'\nRequire stack:\n- /var/task/user.js\n- /var/task/index.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module './shared/caregiverAccessUtils'","Require stack:","- /var/task/user.js","- /var/task/index.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1061:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)","    at async start (file:///var/runtime/index.mjs:1256:23)","    at async file:///var/runtime/index.mjs:1262:1"]}
r
Hi @Charlie from Imagine - yes you have to set up the bundler step to inline imported functions. I can updated our medplum-demo-bots repository with the appropriate configuration
c
Thanks Rahul! Figured, have been working on modifying to use esbuild.
r
Yep. Here's an example esbuild config
Copy code
import esbuild from 'esbuild';
import { glob } from 'glob';

// Find all TypeScript files in your source directory
const entryPoints = glob.sync('./src/**/*.ts').filter((file) => !file.endsWith('test.ts'));

const external = ['form-data', 'node-fetch', 'pdfmake', 'ssh2', 'ssh2-sftp-client', 'dotenv', '@medplum/core'];

// Define the esbuild options
const esbuildOptions = {
  entryPoints: entryPoints,
  bundle: true, // Bundle imported functions
  outdir: './dist', // Output directory for compiled files
  platform: 'node', // or 'node', depending on your target platform
  loader: {
    '.ts': 'ts', // Load TypeScript files
  },
  resolveExtensions: ['.ts'],
  external,
  format: 'cjs', // Set output format as ECMAScript modules
  target: 'es2020', // Set the target ECMAScript version
  tsconfig: 'tsconfig.build.json',
};

// Build using esbuild
esbuild
  .build(esbuildOptions)
  .then(() => {
    console.log('Build completed successfully!');
  })
  .catch((error) => {
    console.error('Build failed:', JSON.stringify(error, null, 2));
    process.exit(1);
  });
It's important that the compilation output commonjs
159 Views