agracio / edge-js

Run .NET and Node.js code in-process on Windows, macOS, and Linux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Help with setting up edge-js inside a next.js project

Odisseuss opened this issue · comments

Hello!

I am trying to set up edge-js to use it inside a next.js project. I have cloned the edge-js-quick-start project, added a new c# file with the code i want to run and tested it from inside the edge-js-quick-start project. It worked out of the box.

Now, to make it work in next.js, this is what i did:

  1. Copied the QuickStart.Core and QuickStart.sln in a folder inside my next.js app
  2. Added EDGE_USE_CORECLR and EDGE_APP_ROOT env variables and set them up as follows:
process.env.EDGE_USE_CORECLR = "1";
process.env.EDGE_APP_ROOT = require("path").join(
  __dirname,
  "../../../.net/QuickStart/QuickStart.Core/bin/Debug/net7.0"
);
  1. Edited the next.config.mjs to update the webpack config as per the docs:
/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    const newConfig = { ...config };
    newConfig.externals = {
      "edge-js": "commonjs2 edge-js",
    };
    newConfig.node = {
      __dirname: true,
      __filename: true,
    };
    return newConfig;
  },
};

export default nextConfig;
  1. Used edge.func to load the dll and try to run the function.

This however results in the following error:

CoreClrEmbedding::Initialize - Failed to initialize CoreCLR, HRESULT: 0x80070057
 ⨯ TypeError: edge.initializeClrFunc is not a function
    at Hero (./src/components/component/hero.tsx:15:27)
    at stringify (<anonymous>)

Not sure what i can do from here. I am assuming there is something going on regarding how next.js bundles the code, since the quickstart example worked fine

Well first thing to do would be to examine your bundled source, see where edge-js ended up.
Also add some console.log(path) statements to lib/edge.js to see where its trying to pick up the paths from.
Specifically you want to know what edgeNative is set to here

edge = require(edgeNative);