Git Product home page Git Product logo

Comments (8)

XmiliaH avatar XmiliaH commented on June 9, 2024

Have you tried without installing coffee-script and setting ignoreWarnings[].message = /Can't resolve 'coffee-script'/ in the webpack config?

from vm2.

BrunoQuaresma avatar BrunoQuaresma commented on June 9, 2024

@XmiliaH I updated the config to be:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    return {
      ...config,
      ignoreWarnings: [/Can't resolve 'coffee-script'/],
    };
  },
};

module.exports = nextConfig;

And now I'm getting the error:

- warn ./node_modules/vm2/lib/resolver-compat.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/vm2/lib/resolver-compat.js
./node_modules/vm2/lib/nodevm.js
./node_modules/vm2/lib/main.js
./node_modules/vm2/index.js
./app/api/script/route.ts

from vm2.

XmiliaH avatar XmiliaH commented on June 9, 2024

Then try to add the additional module.parser.javascript.commonjsMagicComments=true.

from vm2.

BrunoQuaresma avatar BrunoQuaresma commented on June 9, 2024

Updated config:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    return {
      ...config,
      ignore warnings: [/Can't resolve 'coffee-script'/],
      module: {
        ...config.module,
        parser: {
          ...config.module.parser,
          javascript: {
            ...config.module.parser.javascript,
            commonjsMagicComments: true,
          },
        },
      },
    };
  },
};

module.exports = nextConfig;

The warning is gone but when I access the route, I get a 404 error. To be sure it is not being caused by nonrelatedvm2 code, I removed it, and I can confirm the API route works normally. So, looks like for some reason, NextJS is not being able to compile the dependencies correctly.

The route code:

import { NextResponse } from "next/server";
import { NodeVM } from "vm2";

export async function GET() {
  console.log("Enter");
  const vm = new NodeVM({
    timeout: 500,
    sandbox: {},
  });
  console.log("Starting");
  const result = vm.run(`return { a: "Bruno: }`);
  console.log("Result");
  return NextResponse.json({ data: result });
}

The logs:

- wait compiling /api/script/route (client and server)...
- event compiled client and server successfully in 1066 ms (395 modules)
- wait compiling /_error (client and server)...

Unfortunately, looks like there is no debug mode for next dev command.

from vm2.

BrunoQuaresma avatar BrunoQuaresma commented on June 9, 2024

The code that I used to test if it was related to vm2 or not.

import { NextResponse } from "next/server";
import { NodeVM } from "vm2";

export async function GET() {
  console.log("Enter");
  console.log("Starting");
  console.log("Result");
  return NextResponse.json({ data: "a" });
}

The logs:

- event compiled successfully in 242 ms (89 modules)
Enter
Starting
Result

from vm2.

XmiliaH avatar XmiliaH commented on June 9, 2024

While I cannot tell you why it does not work now. You could try a try-catch block and log the exception.
And some notes: NodeVM does not have a timeout option and the return in the sandbox script will not be used.

from vm2.

BrunoQuaresma avatar BrunoQuaresma commented on June 9, 2024

Even with try-catch there is no error available.

import { NextResponse } from "next/server";
import { VM } from "vm2";

export async function GET() {
  try {
    console.log("Enter");
    const vm = new VM({
      timeout: 500,
      sandbox: {},
    });

    console.log("Starting");
    const result = vm.run(`return { a: "Bruno: }`);

    console.log("Result");

    return NextResponse.json({ data: result });
  } catch (error) {
    console.error(error);
  }
}

I really appreciate your help on this but looks like NextJS is missing something. Ty.

from vm2.

BrunoQuaresma avatar BrunoQuaresma commented on June 9, 2024

Closing this for now since NextJS does not provide any helpful information on what is causing the error.

from vm2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.