Shyam Raghuwanshi
Shyam Raghuwanshi

Reputation: 21

No loader is configured for ".node" files: ../rust.linux-x64-gnu.node

I am trying to use rust in aws lambda so I am using napi for building pre-compiled Node.js addons in Rust or for using rust from nodejs

but when I am trying to get the functions that i converted from rust to js, I am getting this error when i am trying to deploy into aws lambda

Bundling asset MyAppStack/lambda/Code/Stage...
✘ [ERROR] No loader is configured for ".node" files: ../rust.linux-x64-gnu.node

    ../index.js:188:38:
      188 │               nativeBinding = require('./rust.linux-x64-gnu.node')
          ╵                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 error

That's how I am trying to use the code

import { Hono } from 'hono'
import { handle } from 'hono/aws-lambda'
import {scrapeTextFromUrls} from "@gen-fellow/rust-lib"
const app = new Hono()

app.get('/', async (c) => {
    const urls = ["https://www.google.com"]
    const text = scrapeTextFromUrls(urls) //this is the function that is being converted from Rust to JS
    return c.json(text)
})

export const handler = handle(app)

in this file i am getting the error No loader is configured for ".node" files: ../rust.linux-x64-gnu.node

see the highlighted part of code

/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */

/* auto-generated by NAPI-RS */

const { existsSync, readFileSync } = require('fs')
const { join } = require('path')

const { platform, arch } = process

let nativeBinding = null
let localFileExisted = false
let loadError = null

function isMusl() {
  // For Node 10
  if (!process.report || typeof process.report.getReport !== 'function') {
    try {
      const lddPath = require('child_process').execSync('which ldd').toString().trim()
      return readFileSync(lddPath, 'utf8').includes('musl')
    } catch (e) {
      return true
    }
  } else {
    const { glibcVersionRuntime } = process.report.getReport().header
    return !glibcVersionRuntime
  }
}

switch (platform) {
  case 'android':
    switch (arch) {
      case 'arm64':
        localFileExisted = existsSync(join(__dirname, 'rust.android-arm64.node'))
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.android-arm64.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-android-arm64')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'arm':
        localFileExisted = existsSync(join(__dirname, 'rust.android-arm-eabi.node'))
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.android-arm-eabi.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-android-arm-eabi')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on Android ${arch}`)
    }
    break
  case 'win32':
    switch (arch) {
      case 'x64':
        localFileExisted = existsSync(
          join(__dirname, 'rust.win32-x64-msvc.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.win32-x64-msvc.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-win32-x64-msvc')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'ia32':
        localFileExisted = existsSync(
          join(__dirname, 'rust.win32-ia32-msvc.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.win32-ia32-msvc.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-win32-ia32-msvc')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'arm64':
        localFileExisted = existsSync(
          join(__dirname, 'rust.win32-arm64-msvc.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.win32-arm64-msvc.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-win32-arm64-msvc')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on Windows: ${arch}`)
    }
    break
  case 'darwin':
    localFileExisted = existsSync(join(__dirname, 'rust.darwin-universal.node'))
    try {
      if (localFileExisted) {
        nativeBinding = require('./rust.darwin-universal.node')
      } else {
        nativeBinding = require('@gen-fellow/rust-lib-darwin-universal')
      }
      break
    } catch {}
    switch (arch) {
      case 'x64':
        localFileExisted = existsSync(join(__dirname, 'rust.darwin-x64.node'))
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.darwin-x64.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-darwin-x64')
          }
        } catch (e) {
          loadError = e
        }
        break
      case 'arm64':
        localFileExisted = existsSync(
          join(__dirname, 'rust.darwin-arm64.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.darwin-arm64.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-darwin-arm64')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on macOS: ${arch}`)
    }
    break
  case 'freebsd':
    if (arch !== 'x64') {
      throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
    }
    localFileExisted = existsSync(join(__dirname, 'rust.freebsd-x64.node'))
    try {
      if (localFileExisted) {
        nativeBinding = require('./rust.freebsd-x64.node')
      } else {
        nativeBinding = require('@gen-fellow/rust-lib-freebsd-x64')
      }
    } catch (e) {
      loadError = e
    }
    break
  case 'linux':
    switch (arch) {
      case 'x64':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-x64-musl.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-x64-musl.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-x64-musl')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-x64-gnu.node')
          )
          try {
            if (localFileExisted) {
             // ------------------here is the problem --------------------------------
              nativeBinding = require('./rust.linux-x64-gnu.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-x64-gnu')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 'arm64':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm64-musl.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm64-musl.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm64-musl')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm64-gnu.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm64-gnu.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm64-gnu')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 'arm':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm-musleabihf.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm-musleabihf.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm-musleabihf')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-arm-gnueabihf.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-arm-gnueabihf.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-arm-gnueabihf')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 'riscv64':
        if (isMusl()) {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-riscv64-musl.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-riscv64-musl.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-riscv64-musl')
            }
          } catch (e) {
            loadError = e
          }
        } else {
          localFileExisted = existsSync(
            join(__dirname, 'rust.linux-riscv64-gnu.node')
          )
          try {
            if (localFileExisted) {
              nativeBinding = require('./rust.linux-riscv64-gnu.node')
            } else {
              nativeBinding = require('@gen-fellow/rust-lib-linux-riscv64-gnu')
            }
          } catch (e) {
            loadError = e
          }
        }
        break
      case 's390x':
        localFileExisted = existsSync(
          join(__dirname, 'rust.linux-s390x-gnu.node')
        )
        try {
          if (localFileExisted) {
            nativeBinding = require('./rust.linux-s390x-gnu.node')
          } else {
            nativeBinding = require('@gen-fellow/rust-lib-linux-s390x-gnu')
          }
        } catch (e) {
          loadError = e
        }
        break
      default:
        throw new Error(`Unsupported architecture on Linux: ${arch}`)
    }
    break
  default:
    throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}

if (!nativeBinding) {
  if (loadError) {
    throw loadError
  }
  throw new Error(`Failed to load native binding`)
}

const { scrapeWebsite, scrapeTextFromUrls, ModelTypes, generateEmbeddings } = nativeBinding

module.exports.scrapeWebsite = scrapeWebsite
module.exports.scrapeTextFromUrls = scrapeTextFromUrls
module.exports.ModelTypes = ModelTypes
module.exports.generateEmbeddings = generateEmbeddings

I would like to know how I should proceed further from here... Please let me know if something is missing i will send it Thank You

Upvotes: 1

Views: 93

Answers (1)

Shyam Raghuwanshi
Shyam Raghuwanshi

Reputation: 21

I got the answer, we can add externalModules like this in my-app-stack.ts

bundling: {
  externalModules: ["@gen-fellow/rust-lib"],
}
//my-app-stack.ts
import * as cdk from 'aws-cdk-lib'
import { Construct } from 'constructs'
import * as lambda from 'aws-cdk-lib/aws-lambda'
import * as apigw from 'aws-cdk-lib/aws-apigateway'
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'

export class MyAppStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const fn = new NodejsFunction(this, 'lambda', {
      entry: 'lambda/index.ts',
      handler: 'handler',
      runtime: lambda.Runtime.NODEJS_20_X,
      bundling: {
        externalModules: ["@gen-fellow/rust-lib"],
      }
    })
    fn.addFunctionUrl({
      authType: lambda.FunctionUrlAuthType.NONE,
    })
    new apigw.LambdaRestApi(this, 'myapi', {
      handler: fn,
    })
  }
}

Upvotes: 0

Related Questions