简体   繁体   English

我正在尝试链接 Node.js+Webpack+MySQL,但它卡在 fs 模块中。 帮我

[英]I'm trying to link Node.js+Webpack+MySQL, but it's stuck in fs module. Help me

I tried to run the program by linking MySQL and Node.js, but fs cannot be loaded due to the following errors.我尝试通过链接MySQL和Node.js来运行程序,但是由于以下错误无法加载fs。

Module not found: Error: Can't resolve 'fs'找不到模块:错误:无法解析“fs”

By the way, adding target:'node' , causes a global is not defined error.顺便说一句,添加target:'node'会导致全局未定义错误。

and add node {fs:'empty'} , causes a createConnection is not a function error.并添加节点 {fs:'empty'} ,导致createConnection is not a function错误。

What should I do now我现在该怎么办

webpack.config.js webpack.config.js

const path = require('path');

module.exports = (env, argv) => {
    return {
        mode: 'production',
        entry: {
            index : path.join(__dirname, 'src' ,'index.ts'),
        },
        output: {
            path: path.join(__dirname, 'www'),
            filename: 'game.js',
            library: 'game',
            libraryTarget: 'umd'
        },
        resolve: {
            extensions: ['.ts', '.js'],
            modules: [
                path.resolve(__dirname, 'src'),"node_modules"
            ]
        },
        module: {
            rules: [
                {
                    test : /\.ts$/,
                    use: [{ loader: 'ts-loader'}]
                }
            ]
        }
    }
};

mysql function mysql function

function get_connection():void {
    console.log("mysql : ",mysql)
    console.log("mysql.createConnection", mysql.createConnection)

    connection = mysql.createConnection({
        host: "host",
        user: "user",
        password: "pass",
        database: "database"
    }).then(conn => {
        console.log('promise-mysql createConnection.');
        connection = conn;
        return conn;
    }).catch(err => {
        console.log("promise fail:",err)
    })
}

As you're keen to use webpack to bundle for node server code, you shouldn't bundle node_nodules by using this helper webpack-node-externals .由于您热衷于使用 webpack 来捆绑节点服务器代码,因此不应使用此帮助webpack-node-externals来捆绑 node_nodules 。 So try more with these options:因此,请尝试使用以下选项进行更多尝试:

 var nodeExternals = require('webpack-node-externals')

 target: 'node',
 externals: [
   nodeExternals(),
 ],
 node: {
   fs: "empty",
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: 不能在模块外使用 import 语句。 我正在使用 typescript 和 discord js - SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js 我正在尝试使用纱线链接和其他文件夹说我它不是一个模块 - I'm trying to use yarn link and other folder says me it is not a module 在React.js,node.js,webpack,babel,express中使用fs模块 - Use fs module in React.js,node.js, webpack, babel,express 将node.js fs与Webpack一起使用 - Use node.js fs with Webpack 我在尝试为 Stripe 支付系统设置 Node.js 时迷失了方向,老板用 dealine 放弃了我的项目 - I'm lost trying to setup Node.js for Stripe payment system, boss dropped project on me with dealine 为什么即使我不尝试解析,node.js 也会给我一个解析错误? - Why does node.js give me a parsing error even when I'm not trying to parse? 我对node.js中的模块感到困惑 - I'm confused with the module in node.js 我正在尝试创建一个简单的模块但不起作用。 节点JS - I'm trying create a simple module but doesn't work. Node JS 运行main.js时出现错误。 “找不到模块'app'”。 谁能帮我 - I'm getting an error while running main.js. “ Cannot find module 'app' ”. Could anyone help me out POST 请求正在使用 POSTMAN,但是当我尝试使用 Axios 从浏览器(UI)调用相同的链接时,它显示 404 错误 | node.js - POST request is working using POSTMAN, but when I'm trying to call the same link using Axios from browser(UI) it's showing 404 error | node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM