【问题标题】:I'm trying to link Node.js+Webpack+MySQL, but it's stuck in fs module. Help me我正在尝试链接 Node.js+Webpack+MySQL,但它卡在 fs 模块中。帮我
【发布时间】:2020-11-09 07:01:13
【问题描述】:

我尝试通过链接MySQL和Node.js来运行程序,但是由于以下错误无法加载fs。

找不到模块:错误:无法解析“fs”

顺便说一句,添加 target:'node' 会导致 global is not defined 错误。

并添加 node {fs:'empty'},会导致 createConnection is not a function 错误。

我现在该怎么办

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 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)
    })
}

【问题讨论】:

    标签: javascript mysql node.js typescript webpack


    【解决方案1】:

    由于您热衷于使用 webpack 来捆绑节点服务器代码,因此您不应使用此帮助程序 webpack-node-externals 来捆绑 node_nodules。因此,请尝试以下选项:

     var nodeExternals = require('webpack-node-externals')
    
     target: 'node',
     externals: [
       nodeExternals(),
     ],
     node: {
       fs: "empty",
     }
    

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 2022-01-20
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多