You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core集成Node.js(Webpack/Browserify)调用函数失败求助

.NET Core 集成 Node.js 模块调用失败问题

尝试多日将 .NET Core 与 Node.js 结合使用,先后试过 Webpack 和 Browserify 打包,但均未成功。奇怪的是,能在 Webpack 生成的 JavaScript 文件中看到函数定义,却无法调用它。

补充说明:并非 Visual Studio 中默认的 .NET React 应用,只是普通的 .NET Web 应用。我添加了存放 Node 相关文件的文件夹,用 Webpack 打包后生成 .js 文件并引入到 .NET Web 应用中。不需要 Node.js 做 UI 开发,仅需通过 UI 端 JS 调用以太坊区块链相关的 Node 模块,这些模块无法通过 <script type="text/javascript" src="web3.min.js"></script> 引入,只能用 import { ethers } from "ethers"; 或 var ethers = require('ethers'); 获取。


相关代码及操作

temp.js(入口文件)

function log(){
  console.log("log from node");
}

module.exports = log;

webpack.config.js 配置

const path = require('path');

module.exports = {
  resolve: {
    alias: {
      '@aliasName': path.resolve(__dirname, '../wwwroot/tempFolder')
    },
    modules: [
      'node_modules',
      path.resolve(__dirname, './node_modules'),
    ],
  },
  mode: 'production',
  entry: '../wwwroot/tempFolder/temp.js',
  output: {
    filename: 'temp_bundle.js',
    path: path.resolve(__dirname, '../wwwroot/tempFolder'),
    libraryTarget: 'var',
    library: 'EntryPoint'
  },
  optimization: {
    minimize: false
  }
};

执行的打包命令

# Browserify 打包命令
browserify temp.js -o temp_bundle.js

# Webpack 打包命令
npx webpack --config webpack.config.js

.NET Core 页面代码(index.cshtml)

<script src="temp_bundle.js"></script>
<script>log()</script>

报错信息

ReferenceError: log is not defined
    at functionName ((index):170:13)

内容的提问来源于stack exchange,提问作者Hovo Pinelopi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 20:42:17