首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用webpack导入mongodb到前端js文件会触发错误

用webpack导入mongodb到前端js文件会触发错误
EN

Stack Overflow用户
提问于 2018-08-27 11:10:34
回答 2查看 583关注 0票数 0

我正在使用truffle webpack box创建一个Dapp,现在我想使用mongoDB来存储和检索信息。在导入MongoDB模块时,webpack服务器会抛出一个错误,没有import from 'mongodb'位,它运行得很好。据我所知,我的app.js文件在客户端,我不能简单地导入它,但这就是Webpack的作用,另外我还在本地安装了mongodb模块,比如:npm install mongodb --savenpm install mongodb --save-dev

这是错误:

ERROR in ./~/mongodb/lib/url_parser.js
Module not found: Error: Can't resolve 'dns' in '/home/nik/ethereumStockExchange/node_modules/mongodb/lib'
 @ ./~/mongodb/lib/url_parser.js 7:8-22
 @ ./~/mongodb/lib/operations/mongo_client_ops.js
 @ ./~/mongodb/lib/mongo_client.js
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

ERROR in ./~/mongodb-core/lib/uri_parser.js
Module not found: Error: Can't resolve 'dns' in '/home/nik/ethereumStockExchange/node_modules/mongodb-core/lib'
 @ ./~/mongodb-core/lib/uri_parser.js 4:12-26
 @ ./~/mongodb-core/index.js
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

ERROR in ./~/mongodb-core/lib/connection/connection.js
Module not found: Error: Can't resolve 'net' in '/home/nik/ethereumStockExchange/node_modules/mongodb-core/lib/connection'
 @ ./~/mongodb-core/lib/connection/connection.js 5:8-22
 @ ./~/mongodb-core/index.js
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

ERROR in ./~/mongodb-core/lib/connection/connection.js
Module not found: Error: Can't resolve 'tls' in '/home/nik/ethereumStockExchange/node_modules/mongodb-core/lib/connection'
 @ ./~/mongodb-core/lib/connection/connection.js 6:8-22
 @ ./~/mongodb-core/index.js
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

ERROR in ./~/require_optional/~/resolve-from/index.js
Module not found: Error: Can't resolve 'module' in '/home/nik/ethereumStockExchange/node_modules/require_optional/node_modules/resolve-    from'
 @ ./~/require_optional/~/resolve-from/index.js 3:13-30
 @ ./~/require_optional/index.js
 @ ./~/mongodb-core/index.js
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

ERROR in ./~/require_optional/index.js
Module not found: Error: Can't resolve 'fs' in '/home/nik/ethereumStockExchange/node_modules/require_optional'
 @ ./~/require_optional/index.js 2:7-20
 @ ./~/mongodb-core/index.js
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

ERROR in ./~/mongodb/lib/gridfs/grid_store.js
Module not found: Error: Can't resolve 'fs' in '/home/nik/ethereumStockExchange/node_modules/mongodb/lib/gridfs'
 @ ./~/mongodb/lib/gridfs/grid_store.js 42:11-24
 @ ./~/mongodb/index.js
 @ ./app/javascripts/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./app/javascripts/app.js

webpack: Failed to compile.

这是在我的index.html中作为脚本引用的app.js文件

// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract';
import listing_artifacts from '../../build/contracts/Listing.json';

import Mongo from 'mongodb';

// Updates the address and balance
window.updateBalance = function() {
  console.log("Updating balance\nDefault account:  " + web3.eth.defaultAccount + "\nCurrent balance:  " + web3.eth.getBalance(web3.eth.defaultAccount));
  $("#addressAndBalance").html("ADDRESS:&nbsp&nbsp&nbsp" + web3.eth.defaultAccount + "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp BALANCE:&nbsp&nbsp&nbsp" + web3.eth.getBalance(web3.eth.defaultAccount).toString());
}

$(document).ready(function() {
  // This is used on startup in order to set up
  // the connection the Ethereum node
  if (typeof web3 !== 'undefined') {
    console.warn("Using web3 detected from external source like Metamask")
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    console.warn("No web3 detected. Falling back to http://localhost:8545.");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    window.web3 = new Web3(new    Web3.providers.HttpProvider("http://localhost:8545"));
 }

  console.log("Accounts:  " + web3.eth.accounts);
   web3.eth.defaultAccount = web3.eth.accounts[0];
  updateBalance();
});

这是我的webpack.config.js

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
//const mongo = require('mongodb');

module.exports = {
  entry: './app/javascripts/app.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'app.js'
  },
  plugins: [
    // Copy our app's index.html to the build folder.
    new CopyWebpackPlugin([
      { from: './app/index.html', to: "index.html" }
    ])
  ],
  module: {
    rules: [
      {
       test: /\.css$/,
       use: [ 'style-loader', 'css-loader' ]
      }
    ],
    loaders: [
      { test: /\.json$/, use: 'json-loader' },
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['es2015'],
          plugins: ['transform-runtime']
         }
      }
     ]
  }
}

这是我的package.json文件:

{
  "name": "truffle-init-webpack",
  "version": "0.0.2",
  "description": "Frontend example using truffle v3",
  "scripts": {
    "lint": "eslint ./",
    "build": "webpack",
    "dev": "webpack-dev-server"
  },
  "author": "Douglas von Kohorn",
  "license": "MIT",
  "devDependencies": {
    "babel-cli": "^6.22.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^6.1.2",
    "babel-loader": "^6.2.10",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-env": "^1.1.8",
    "babel-preset-es2015": "^6.22.0",
    "babel-register": "^6.22.0",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.26.1",
    "eslint": "^3.14.0",
    "eslint-config-standard": "^6.0.0",
    "eslint-plugin-babel": "^4.0.0",
    "eslint-plugin-mocha": "^4.8.0",
    "eslint-plugin-promise": "^3.0.0",
    "eslint-plugin-standard": "^2.0.0",
    "fs": "0.0.1-security",
    "html-webpack-plugin": "^2.28.0",
    "json-loader": "^0.5.4",
    "mongodb": "^3.1.4",
    "style-loader": "^0.13.1",
    "truffle-contract": "^1.1.11",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.3.0"
  },
  "dependencies": {
    "web3": "^0.20.2"
  }
}
EN

回答 2

Stack Overflow用户

发布于 2018-08-27 12:22:33

您无法在客户端代码中导入mongodb模块。是的,当然!您可以使用web pack导入节点模块,但由于mongodb是nodejs库,因此不能在这里导入。但是node_modules中的任何客户端库都是可以的。

票数 0
EN

Stack Overflow用户

发布于 2018-08-28 06:40:43

我没有在客户端使用mongoDB (这可以通过使用http rest api实现),而是使用了内置在浏览器中的indexedDB。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52032086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档