Я использую трюфельный пакет для создания Dapp, теперь я хочу использовать mongoDB для хранения и извлечения информации.При импорте модуля MongoDB webpack sever выдает ошибку, без бита import from 'mongodb'
все работает нормально.Из того, что я могу понять, так как мой app.js
файл находится на стороне клиента, я не могу просто импортировать его, но это то, для чего предназначен Webpack, плюс я дополнительно установил mongodb
модуль локально, например: npm install mongodb --save
и npm 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.
Это мой app.js
файл, который упоминается как скрипт в моем index.html
// 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:   " + web3.eth.defaultAccount + "       BALANCE:   " + 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"
}
}