agracio / edge-js

Run .NET and Node.js code in-process on Windows, macOS, and Linux
MIT License
618 stars 93 forks source link

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

Closed Odisseuss closed 3 months ago

Odisseuss commented 5 months ago

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"
    );
  3. 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;

4. 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 ()



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
agracio commented 5 months ago

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 https://github.com/agracio/edge-js/blob/7e08b8ccfc0e7b4a837870ef6322779b16c231d6/lib/edge.js#L53