簡體   English   中英

節點模塊 Knex.js 和 browserify 問題

[英]Node module Knex.js and browserify issue

我有一個名為 product 的節點模塊,它使用 knex.js 和 sqlite3,它在節點上運行良好。 現在我正在嘗試使用 browserify 在瀏覽器上運行它,方法是將 browserify 生成的 bundle js 文件包含在 html 中,如下所示

<script src="product.bundle.js"></script>

以下是節點和 npm 版本:

Node: 12.18.0
npm: 6.13.7

Package.json:

{
  "name": "product",
  "version": "1.0.0",
  "description": "knexjs sample module",
  "main": "product.js",
  "dependencies": {
    "knex": "^0.21.1",
    "sqlite3": "^4.2.0",
    "browserify": "^16.5.1"
  }
}

產品.js

const sqlite = require( 'sqlite3' )

const knex = require('knex')({
    client: 'sqlite3',
    connection: {
        filename: "./mysqlitedb.sqlite"
      }
});

module.exports = {
    init: function(){
        knex.schema.createTable('products', (table) => {
            table.increments('id')
            table.string('code')
            table.string('description')
            table.string('price')
        }).then(() => console.log("table created"))
            .catch((err) => { console.log(err); throw err })
            .finally(() => {
                knex.destroy();
            });
    },
    insertIntoProductsTable: function(){
        const products = [
            { code: 'Product code 1', description:'Product desc 1', price: '11.00' }
        ]
        
        knex('products').insert(products).then(() => console.log("data inserted"))
            .catch((err) => { console.log(err); throw err })
            .finally(() => {
                knex.destroy();
            });
    },
    getAllProducts: function(){
        knex.raw( 'select * from products' )
        .then( function( products ){
            console.log( products )
        })
        .catch( function( err ){
            console.log( err )
        })
        .finally(() => {
            knex.destroy();
        });
    }
}

使用命令“ browserify product.js -o product.bundle.js ”得到product.bundle.js 文件。 當嘗試在瀏覽器上加載時出現以下錯誤:

在此處輸入圖像描述

在此處輸入圖像描述

請讓我知道,如果我遺漏了任何東西,或者是否有任何鏈接可以檢查是否實施了類似的東西?

提前致謝。

您的錯誤告訴您瀏覽器環境中不存在stream模塊(通常存在於節點環境中)。 許多節點先決條件(如fs )都是如此。 SQLite3 也有一個本地編譯的代碼組件,它肯定不會在瀏覽器中運行。 您最好將數據存儲在服務器上,或者使用不同的技術進行本地存儲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM