- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当我尝试在我的 Node 应用程序中使用 mongoose connect 时,Webpack 抛出以下错误。
最初还有几个错误,例如,
Module not found: Error: Cannot resolve module 'fs'
在我的 webpack 配置文件中进行以下更改就可以了,
我在我的 webpack 配置文件中添加了 node-loader 和 node 对象。
Node :{ 控制台:'空', fs:'空', 净:'空', tls:'空'},
但是下面的错误仍然存在。知道如何解决吗?
WARNING in ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/index.js
Critical dependencies:
63:18-42 the request of a dependency is an expression
71:20-44 the request of a dependency is an expression
78:35-67 the request of a dependency is an expression
@ ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/index.js 63:18-42 71:20-44 78:35-67
WARNING in ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/README.md
Module parse failed: /Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/require_optional/README.md Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected character '#' (1:0)
at Parser.pp.raise (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:920:13)
at Parser.pp.getTokenFromCode (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:2813:8)
at Parser.pp.readToken (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:2508:15)
at Parser.pp.nextToken (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:2500:71)
at Parser.parse (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1615:10)
at Object.parse (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:882:44)
at Parser.parse (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
@ ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional ^\.\/.*$
WARNING in ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/LICENSE
Module parse failed: /Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/require_optional/LICENSE Unexpected token (1:40)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (1:40)
at Parser.pp.raise (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:920:13)
at Parser.pp.unexpected (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1483:8)
at Parser.pp.semicolon (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1462:73)
at Parser.pp.parseExpressionStatement (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1976:8)
at Parser.pp.parseStatement (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1754:188)
at Parser.pp.parseTopLevel (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1648:21)
at Parser.parse (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:1616:17)
at Object.parse (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/node_modules/acorn/dist/acorn.js:882:44)
at Parser.parse (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/webpack/lib/NormalModule.js:104:16)
@ ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional ^\.\/.*$
ERROR in ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/~/resolve-from/index.js
Module not found: Error: Cannot resolve module 'module' in /Users/nitesh/Documents/learnReact/day1/r3-foundation-boilerplate/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/node_modules/require_optional/node_modules/resolve-from
@ ./~/mongoose/~/mongodb/~/mongodb-core/~/require_optional/~/resolve-from/index.js 3:13-30
Child extract-text-webpack-plugin:
chunk {0} extract-text-webpack-plugin-output-filename 221 kB [rendered]
[0] ./~/css-loader!./~/postcss-loader!./src/styles/app.css 219 kB {0} [built]
[1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built]
Child extract-text-webpack-plugin:
chunk {0} extract-text-webpack-plugin-output-filename 8.56 kB [rendered]
[0] ./~/css-loader!./~/postcss-loader!./src/styles/styles.css 7.06 kB {0} [built]
[1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built]
Child extract-text-webpack-plugin:
chunk {0} extract-text-webpack-plugin-output-filename 7.92 kB [rendered]
[0] ./~/css-loader!./~/postcss-loader!./src/styles/slider.css 6.42 kB {0} [built]
[1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built]
Child extract-text-webpack-plugin:
chunk {0} extract-text-webpack-plugin-output-filename 234 kB [rendered]
[0] ./~/css-loader!./~/postcss-loader!./src/styles/app_override.css 232 kB {0} [built]
[1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built]
Child extract-text-webpack-plugin:
Asset Size Chunks Chunk Names
404a525502f8e5ba7e93b9f02d9e83a9.eot 75.2 kB
926c93d201fe51c8f351e858468980c3.woff2 70.7 kB
891e3f340c1126b4c7c142e5f6e86816.woff 89.1 kB
fb650aaf10736ffb9c4173079616bf01.ttf 151 kB
bae4a87c1e5dff40baa3f49d52f5347a.svg 386 kB
chunk {0} extract-text-webpack-plugin-output-filename 41.4 kB [rendered]
[0] ./~/css-loader!./~/postcss-loader!./src/styles/index.css 264 bytes {0} [built]
[1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built]
[2] ./~/css-loader!./~/font-awesome/css/font-awesome.css 39.1 kB {0} [built]
[3] ./~/font-awesome/fonts/fontawesome-webfont.eot 82 bytes {0} [built]
[4] ./~/font-awesome/fonts/fontawesome-webfont.eot?v=4.6.1 82 bytes {0} [built]
[5] ./~/font-awesome/fonts/fontawesome-webfont.woff2?v=4.6.1 84 bytes {0} [built]
[6] ./~/font-awesome/fonts/fontawesome-webfont.woff?v=4.6.1 83 bytes {0} [built]
[7] ./~/font-awesome/fonts/fontawesome-webfont.ttf?v=4.6.1 82 bytes {0} [built]
[8] ./~/font-awesome/fonts/fontawesome-webfont.svg?v=4.6.1 82 bytes {0} [built]
Child extract-text-webpack-plugin:
chunk {0} extract-text-webpack-plugin-output-filename 88.8 kB [rendered]
[0] ./~/css-loader!./~/postcss-loader!./~/sass-loader?outputStyle=expanded!./src/styles/foundation.scss 87.3 kB {0} [built]
[1] ./~/css-loader/lib/css-base.js 1.51 kB {0} [built]
Module not found: Error: Cannot resolve module 'module' mongodb
webpack.config.js
var path = require('path')
var webpack = require('webpack')
var autoprefixer = require('autoprefixer')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var assetPath = '/assets/'
var absolutePath = path.join(__dirname, 'build', assetPath)
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'webpack-hot-middleware/client',
'./src/index'
],
target: 'node-webkit',
output: {
path: absolutePath,
filename: 'bundle.js',
publicPath: assetPath
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new ExtractTextPlugin("bundle.css")
],
module: {
loaders: [
{
test: /\.js$/,
loaders: [ 'babel' ],
exclude: /node_modules/,
include: path.join(__dirname, 'src')
},
// fonts and svg
{ test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
{ test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" },
{
// images
test: /\.(ico|jpe?g|png|gif)$/,
loader: "file"
},
{
// for some modules like foundation
test: /\.scss$/,
exclude: [/node_modules/], // sassLoader will include node_modules explicitly
loader: ExtractTextPlugin.extract("style", "css!postcss!sass?outputStyle=expanded")
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract("style", "css!postcss")
},
{ test: /\.json$/, loader: 'json-loader' },
{
test: /\.node$/,
loader: 'node-loader'
}
]
},
resolve: {
extensions: [ '', '.js', '.json', '.jsx', '.es6', '.babel', '.node'],
modulesDirectories: [ 'node_modules', 'app' ]
},
node: {
console: true,
fs: 'empty',
net: 'empty',
tls: 'empty'
},
postcss: function(webpack) {
return [
autoprefixer({browsers: ['last 2 versions', 'ie >= 9', 'and_chr >= 2.3']})
]
},
sassLoader: {
includePaths: [path.resolve(__dirname, "node_modules")]
}
}
package.json
{
"name": "nodeReactMongo",
"version": "1.0.0",
"description": "redux-react-router and foundation boilerplate",
"keywords": [
"redux",
"react",
"router",
"routing",
"frontend",
"client",
"webpack",
"babel",
"sass",
"foundation",
"postcss"
],
"main": "index.js",
"scripts": {
"start": "node server"
},
"author": "nitte93",
"license": "ISC",
"dependencies": {
"axios": "^0.11.1",
"classnames": "^2.2.0",
"firebase": "^3.0.3",
"font-awesome": "^4.3.0",
"foundation-sites": "^6.1.2",
"json-loader": "^0.5.4",
"mongodb": "^2.1.20",
"mongoose": "^4.4.19",
"react": "^0.14.5",
"react-addons-update": "^0.14.7",
"react-dom": "^0.14.7",
"react-modal": "^1.3.0",
"react-router": "2.0.0-rc5",
"react-router-redux": "^2.1.0",
"redux": "^3.2.1",
"redux-form": "^5.2.5",
"redux-logger": "^2.5.2",
"redux-thunk": "^1.0.3",
"rethinkdb": "^2.3.1",
"underscore": "^1.8.3",
"what-input": "^1.1.4"
},
"optionalDependencies": {},
"devDependencies": {
"autoprefixer": "^6.3.2",
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-plugin-transform-runtime": "^6.5.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-preset-stage-0": "^6.5.0",
"babel-runtime": "^6.9.0",
"css-loader": "^0.23.1",
"express": "^4.13.3",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.8.5",
"jquery": "^2.2.4",
"node-sass": "^3.4.2",
"postcss-loader": "^0.8.0",
"react-redux": "^4.4.5",
"redux-devtools": "^3.1.0",
"redux-devtools-dock-monitor": "^1.0.1",
"redux-devtools-log-monitor": "^1.0.3",
"sass-loader": "^3.1.2",
"script-loader": "^0.6.1",
"serve-static": "^1.10.2",
"style-loader": "^0.13.0",
"url-loader": "^0.5.7",
"webpack": "^1.9.11",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.2.0"
}
}
在我执行 import mongoose from 'mongoose'
后立即在我的一个 react 组件文件中。我收到了这个错误。
import React, { Component, PropTypes } from 'react'
import request from '../api/requestHandler'
import { reduxForm } from 'redux-form'
//import rethink from 'rethinkdb'
import mongoose from 'mongoose'
最佳答案
当 Webpack 捆绑您的模块时,它会遵循您已导入(或需要)的模块的依赖链,并拉入其所有依赖项,并将它们一直捆绑到链的末尾。
如果有一个文件不知道如何在该依赖链中加载,则会抛出这种类型的错误。
这有时可以通过添加一个知道如何加载此类依赖项的加载程序来解决。但是,如果依赖项是非本地模块,则 Webpack 无法加载它。一些加载器知道如何通过 stub 和排除非本地部分来加载具有非本地依赖项的模块,以便加载。例如,在 fs
模块中,您不需要能够从磁盘读取和写入文件,因为浏览器无法做到这一点,因此无需包含该部分。
这就提出了一个问题:浏览器中需要 mongoose 模块的哪些功能?你能只包含那个功能而不是整个 mongoose 模块吗?
如果你能做到这一点,那么你可能能够解决 2 个问题:
例如,我最近需要在客户端使用 mongodb ObjectId 生成器。我发现 Webpack 无法处理 import mongodb from 'mongodb'
组件,因此深入研究了依赖项,我发现 mongodb
依赖于 mongodb-core
依赖于 bson
,它具有我需要的 ObjectId
方法。
通过仅导入该依赖链的 bson
组件,我解决了 Webpack 问题并使我的包更小。
如果您使用的是 Npm 3,那么如果您已经在使用 mongoose 或 mongodb,那么 bson
很有可能安装在 node_modules
的根目录中,这样您就可以import
而不在 package.json
中明确引用它。这显然会带来风险,如果上层依赖项停止依赖它,那么您的构建将中断,您需要独立 npm install
它。使用这种方法的优点是您将始终使用与上层依赖项使用的相同版本的 bson
,这可能很重要。
关于node.js - 找不到模块 : Error: Cannot resolve module 'module' mongodb while bundling with webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506714/
我使用 resolver() 作为 socket() 的替代方法,因为我发现当多个连接建立到不同的 IP 时,它最终会停止工作。 无论如何它会向我返回一个警告,我应该使用 dns.resolver.R
我有这个代码: var promise1 = new Promise(function(resolve, reject) { setTimeout(() => { console
我仍在学习 PHP,我认为我不是母语人士,这并不难理解。 此时,看了一大堆文档,跳入了深水区,于是打开Laravel源文件,一个接一个地看,试图更好地理解MVC的整个实现,包括路由、中间件如何组合成一
public JsonResult GetEvents(double start, double end) { var userName = Session["UserName"] as st
我正在使用 bluebird,我看到了两种将同步函数解析为 Promise 的方法,但我不明白这两种方法之间的区别。看起来堆栈跟踪有点不同,所以它们不仅仅是一个别名,对吧? 那么首选的方式是什么? 方
我写了下面的代码: function readFile(path) { return new Promise(function(resolve, reject){ if(!fs
我正在使用 bluebird,我看到了两种将同步函数解析为 Promise 的方法,但我不明白这两种方法之间的区别。看起来堆栈跟踪有点不同,所以它们不仅仅是一个别名,对吧? 那么首选的方式是什么? 方
在某处读过这个例子: return new Promise( (resolve, reject) => { fs.readFile(file, (err, data) => { if (e
我刚开始学习 React,我一直在尝试让我的 React 应用程序连接到我的数据库 var mysql = require('mysql'); var con = mysql.createConnec
我需要从 $http 调用中返回一个 promise 中的自定义响应,以便我可以链接更多调用。我有两个可用的实现。有人可以解释两者之间的区别,并争论其中一个更好吗? 在 fooService.js 实
免责声明:这里实际上提出了两个问题,但我觉得它们密切相关。 我正在尝试将 promise 对象传递给指令,并且我想在 promise 解析后立即在指令中运行一些初始化代码。 在我的 Controlle
我正在尝试创建类似于 this code 的东西在 boost.asio 示例中找到。 套接字.h: class some_class { private: ... boost
正如我们所知,Promise 构造函数采用一个执行函数,该函数具有两个参数,我们使用它们来生成成功案例或失败案例。今天我在编程,我被卡住了,但后来我解决了这个问题,但我发现了一件事需要理解。 有什么区
我认为 Promise.resolve 和 new Promise(resolve) 可以互换。 考虑一下: A. new RSVP.Promise(function (resolve, reject
我下载了一个 Java 项目,我想研究并从中学习一些东西。当我在另一台计算机上下载它时效果很好,但是当我在我的计算机上尝试时,几乎每个声明和导入都会给出错误消息“* cannot be resolve
我昨天看到了一些有趣的编译器行为,我想我明白为什么会这样,但我想确定一下。所以,我不会写我的推理,只写事实。 请注意,我使用 vector 而不是 string 并不是错字。我是故意这样做的,这样编译
我正在尝试运行 Ember 测试,它给出了这个错误,提示无法找到从 `AppName/resolver 导入的模块 ember-resolver。 我不确定是什么原因造成的。我正在使用 Ember-c
Code#1 和 Code#2 的区别在于:Code#1 使用 resolve(p) 而 Code#2 使用 p.then(()=>resolve()) 。我希望输出序列是不变的,但它们会生成不同的序
IntelliJ IDEA 无法解析内置 JVM 类型和方法的常见原因有哪些?例如,当我将鼠标悬停在 String 上时,工具提示显示“无法解析符号“String””。就好像 IntelliJ 不知道
IntelliJ IDEA 无法解析内置 JVM 类型和方法的常见原因有哪些?例如,当我将鼠标悬停在 String 上时,工具提示显示“无法解析符号“String””。就好像 IntelliJ 不知道
我是一名优秀的程序员,十分优秀!