代码之家  ›  专栏  ›  技术社区  ›  efirvida

将sequelize/sqlite db添加到reactjs应用程序

  •  0
  • efirvida  · 技术社区  · 4 年前

    ERROR in ./node_modules/sequelize/lib/dialects/postgres/hstore.js
    Module not found: Error: Can't resolve 'pg-hstore'
    

    这是我的 package.json

    {
      "name": "xxxxx",
      "version": "1.0.0",
      "description": "xxxx",
      "scripts": {
        "start": "webpack-dev-server --mode=development --open --hot",
        "build": "webpack --mode=production",
      },
      "author": "efirvida",
      "license": "CC0-1.0",
      "devDependencies": {
        "@babel/core": "^7.11.6",
        "@babel/preset-env": "^7.11.5",
        "@babel/preset-react": "^7.10.4",
        "babel-loader": "^8.1.0",
        "sequelize-cli": "^6.2.0",
        "fs": "0.0.1-security",
        "webpack": "^4.44.1",
        "webpack-cli": "^3.3.12",
        "webpack-dev-server": "^3.11.0",
        "html-webpack-plugin": "^4.4.1",
        "style-loader": "^1.2.1",
        "css-loader": "^4.3.0"
      },
      "dependencies": {
        "react": "^16.13.1",
        "react-dom": "^16.13.1",
        "sequelize": "^6.3.5",
        "sqlite": "^4.0.14",
        "sqlite3": "^5.0.0"
      }
    }
    

    webpack.config.js

    const path = require("path");
    const HtmlWebPackPlugin = require("html-webpack-plugin");
    
    module.exports = {
        output: {
            path: path.resolve(__dirname, "build"),
            filename: "bundle.js",
        },
        resolve: {
            modules: [path.join(__dirname, "src"), "node_modules"],
            alias: {
                react: path.join(__dirname, "node_modules", "react"),
            },
        },
        module: {
            rules: [
                {
                    test: /\.(js|jsx)$/,
                    exclude: /node_modules/,
                    use: {
                        loader: "babel-loader",
                    },
                },
                {
                    test: /\.css$/,
                    use: [
                        {
                            loader: "style-loader",
                        },
                        {
                            loader: "css-loader",
                        },
                    ],
                },
            ],
        },
        plugins: [
            new HtmlWebPackPlugin({
                template: "./src/index.html",
            }),
        ],
    };
    

    我的 models/index.js 文件

    "use strict";
    
    const fs = require("fs");
    const path = require("path");
    const Sequelize = require("sequelize");
    const basename = path.basename(__filename);
    const env = process.env.NODE_ENV || "development";
    const config = require(__dirname + "/../config/config.json")[env];
    const db = {};
    
    let sequelize;
    if (config.use_env_variable) {
        sequelize = new Sequelize(process.env[config.use_env_variable], config);
    } else {
        sequelize = new Sequelize(config);
    }
    
    fs.readdirSync(__dirname)
        .filter((file) => {
            return (
                file.indexOf(".") !== 0 &&
                file !== basename &&
                file.slice(-3) === ".js"
            );
        })
        .forEach((file) => {
            const model = require(path.join(__dirname, file))(
                sequelize,
                Sequelize.DataTypes
            );
            db[model.name] = model;
        });
    
    Object.keys(db).forEach((modelName) => {
        if (db[modelName].associate) {
            db[modelName].associate(db);
        }
    });
    
    db.sequelize = sequelize;
    db.Sequelize = Sequelize;
    
    module.exports = db;
    

    index.js ReactJS应用程序的

    // Create database connectio
    const db = require("./models"); // you need the connection to the database and Campus model
    
    const init = async () => {
        await db.sync({ force: true }); // force true will drop the table if it already exists
        console.log("Tables have synced!");
    };
    init();
    

    使用: externals: ['pg', 'sqlite3', 'tedious', 'pg-hstore'],

    ERROR in ./node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
    Module not found: Error: Can't resolve 'fs' in .../models/index.js
    

    使用: externals: {sequelize: 'require("sequelize")'}

    ERROR in ./src/db/models/index.js
    Module not found: Error: Can't resolve 'fs' in .../models/index.js
    
    0 回复  |  直到 4 年前