gpt4 book ai didi

javascript - 我如何在vanilla js中调用node js的函数?

转载 作者:太空宇宙 更新时间:2023-11-03 22:03:53 25 4
gpt4 key购买 nike

我有一个 Node js 函数 -

const BpmnModdle = require('bpmn-moddle')
var bpmn = function () {
var bm = new BpmnModdle()
console.log(bm)
}
module.exports = bpmn

我想在纯原生js中调用这个函数。

到目前为止我已经尝试过-我创建了一个 fileData javascript 文件,在其中尝试调用 bpmn 函数

文件数据.js

function createData(xml, node) {
var bp = bpmn();
console.log(bp)
}

我尝试将两者捆绑在 webpack 中。我的 webpack 配置文件在哪里

module.exports = {
entry: [
'./javascript/examples/editors/js/bpmn.js',
'./javascript/examples/editors/js/app.js',
'./javascript/examples/editors/js/deletes.js',
'./javascript/examples/editors/js/fileData.js',
'./javascript/examples/editors/js/jsonData.js',
'./javascript/examples/editors/js/new.js',
'./javascript/examples/editors/js/open.js',
'./javascript/examples/editors/js/save.js',
'./javascript/examples/editors/js/saveas.js',
'./javascript/examples/editors/src/js/mxClient.js',
'./node_modules/bpmn-moddle/dist/index.js'
],
output: {
path: __dirname,
publicPath: '/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "script-loader"
}
},
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: true,
importLoaders: 1,
localIdentName: "[name]_[local]_[hash:base64]",
sourceMap: true,
minimize: true
}
}
]
}
]
}
};

我无法在纯 js 中调用此函数,并且收到错误消息“bpmn 未定义”。

最佳答案

在调用函数文件中包含bpmn模块,然后调用它。

在您的代码中,您没有告诉 webpack 有关 bpmn 模块依赖项的信息。

要在 webpack 包中添加模块,您必须在调用函数文件/模块中添加模块

示例

像这样创建文件结构。

enter image description here

创建这些文件并粘贴代码。

webpack.config.js

const path = require('path');
module.exports = {

mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},

};

Package.json

{
"name": "Stackoverflow",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"bpmn-moddle": "^6.0.0"
},
"devDependencies": {
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
}
}

src/index.js



import bpmn from './bpmnModdle.js';
function createData(xml, node) {
var bp = bpmn();
console.log(bp)
console.log('Module called');
}

createData();

src/bpmnModdle.js

import BpmnModdle from 'bpmn-moddle';
var bpmn = function () {
var bm = new BpmnModdle();
console.log('bm', bm)
console.log('From inside the module');
return 'exported'
}
export default bpmn;

index.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="../dist/bundle.js"></script>
<title>Document</title>
</head>

<body>

</body>

</html>

运行npm install
运行npm run build

在浏览器中打开index.html文件

I am using ES6 module as the bpmn-moddle package doesn't support commanJS module system.

关于javascript - 我如何在vanilla js中调用node js的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59728063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com