newcat / baklavajs

Graph / node editor in the browser using VueJS
http://baklava.tech
MIT License
1.47k stars 107 forks source link

Unable to use in Nest.js #379

Closed chuteany closed 5 months ago

chuteany commented 5 months ago
  1. create node in MathNode.ts import { defineNode, NodeInterface } from "@baklavajs/core"; import { NumberInterface, SelectInterface } from "@baklavajs/renderer-vue"; export default defineNode({ type: "Math", inputs: { number1: () => new NumberInterface("Number", 1), number2: () => new NumberInterface("Number", 10), operation: () => new SelectInterface("Operation", "Add", ["Add", "Subtract"]).setPort(false), }, outputs: { output: () => new NodeInterface("Output", 0), }, calculate({ number1, number2, operation }) { let output: number; if (operation === "Add") { output = Number(number1) + Number(number2); } else if (operation === "Subtract") { output = Number(number1) - Number(number2); } else { throw new Error(Unknown operation: ${operation}); } return new Promise((resolve, reject) => { setTimeout(() => { resolve({ output }) ; }, 3000); }) }, });

  2. import this file import MathNode from "./MathNode";

  3. register this node const editor = new Editor(); editor.registerNodeType(MathNode, { category: "A-Base" }); 4.Then, the console reported an error. require() of ES Module .\node_modules.pnpm\@baklavajs+renderer-vue@2.3.0\node_modules\@baklavajs\renderer-vue\dist\renderer-vust\renderer-vue.umd.js from .\dist\modules\auth\MathNode.js not supported. renderer-vue.umd.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package that package scope as ES modules. Instead either rename renderer-vue.umd.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "moduletype": "module" to "type": "commonjs" in D:\Programs\SDMS\eims-task\node_modules.pnpm\@baklavajs+renderer-vue@2.3.0\node_modules\@baklavajs\renderer-vue\package.jll .js files ason to treat all .js files as CommonJS (using .mjs for all ES modules instead).

starker-xp commented 5 months ago

Do you have "type": "module" in your package.json ?

chuteany commented 5 months ago

The original configuration of the project is "type": "module". I want to use it in both browser and Node.js environments simultaneously. I cannot change it to "type": "commonjs".

chuteany commented 5 months ago

I modified the original configuration as follows, so it can be used simultaneously in both browser and Node.js environments.

  1. in package.json "type": "module", "main": "./dist/renderer-vue.cjs", "module": "./dist/renderer-vue.es.js", "exports": { ".": { "import": "./dist/renderer-vue.es.js", "require": "./dist/renderer-vue.cjs" } }

  2. in vite.config.js

lib: { ... formats: ["umd", "es", "cjs"], },

chuteany commented 5 months ago

Could you do this Pull Request

newcat commented 5 months ago

Fixed in v2.4.0