【问题标题】:Mongoose not working with webpack猫鼬不使用 webpack
【发布时间】:2017-01-08 17:01:21
【问题描述】:

当我将此代码添加到我的 jsx 文件中时,我正在尝试使用 mongoose 将文档添加到 MongoDB,我也对网页使用 react

var mongoose = require('mongoose')

它给了我一长串错误:

WARNING in ./~/mongoose/lib/drivers/index.js
Critical dependencies:
8:11-74 the request of a dependency is an expression
@ ./~/mongoose/lib/drivers/index.js 8:11-74

WARNING in ./~/mongoose/lib/drivers/SPEC.md
Module parse failed: /home/adam/code/Booking_App/node_modules/mongoose/lib/drivers/SPEC.md Unexpected character '#' (2:0)
You may need an appropriate loader to handle this file type.
    SyntaxError: Unexpected character '#' (2:0)
at Parser.pp$4.raise (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp$7.getTokenFromCode (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2756:10)
at Parser.pp$7.readToken (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2477:17)
at Parser.pp$7.nextToken (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2468:15)
at Parser.parse (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:515:10)
at Object.parse (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/home/adam/code/Booking_App/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/home/adam/code/Booking_App/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/home/adam/code/Booking_App/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /home/adam/code/Booking_App/node_modules/enhanced-resolve/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)
@ ./~/mongoose/lib/drivers ^\.\/.*$

WARNING in ./~/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
@ ./~/require_optional/index.js 63:18-42 71:20-44 78:35-67

WARNING in ./~/require_optional/README.md
Module parse failed: /home/adam/code/Booking_App/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$4.raise (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp$7.getTokenFromCode (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2756:10)
at Parser.pp$7.readToken (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2477:17)
at Parser.pp$7.nextToken (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2468:15)
at Parser.parse (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:515:10)
at Object.parse (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/home/adam/code/Booking_App/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/home/adam/code/Booking_App/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/home/adam/code/Booking_App/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /home/adam/code/Booking_App/node_modules/enhanced-resolve/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)
@ ./~/require_optional ^\.\/.*$

WARNING in ./~/require_optional/LICENSE
Module parse failed: /home/adam/code/Booking_App/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$4.raise (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:2221:15)
at Parser.pp.unexpected (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:603:10)
at Parser.pp.semicolon (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:581:61)
at Parser.pp$1.parseExpressionStatement (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:966:10)
at Parser.pp$1.parseStatement (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:730:24)
at Parser.pp$1.parseTopLevel (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:638:25)
at Parser.parse (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:516:17)
at Object.parse (/home/adam/code/Booking_App/node_modules/webpack/node_modules/acorn/dist/acorn.js:3098:39)
at Parser.parse (/home/adam/code/Booking_App/node_modules/webpack/lib/Parser.js:902:15)
at DependenciesBlock.<anonymous> (/home/adam/code/Booking_App/node_modules/webpack/lib/NormalModule.js:104:16)
at DependenciesBlock.onModuleBuild (/home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:310:10)
at nextLoader (/home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:275:25)
at /home/adam/code/Booking_App/node_modules/webpack-core/lib/NormalModuleMixin.js:259:5
at Storage.finished (/home/adam/code/Booking_App/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
at /home/adam/code/Booking_App/node_modules/enhanced-resolve/node_modules/graceful-fs/graceful-fs.js:78:16
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)
@ ./~/require_optional ^\.\/.*$

ERROR in ./~/resolve-from/index.js
Module not found: Error: Cannot resolve module 'module' in /home/adam/code/Booking_App/node_modules/resolve-from
@ ./~/resolve-from/index.js 3:13-30

这是我的 webpack 配置:

    var path = require('path');
module.exports = {
    context: __dirname,
    entry: './client/app.jsx',
    output: {
        path: path.join(__dirname, './public/'),
        publicPath: './public/',
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.json','.babel', '.node']
    },
    stats: {
        colors: true,
        reasons: true,
        chunks: false
    },

    module: {
        loaders: [
            {
                test: /\.jsx$/,
                loader: 'jsx-loader?insertPragma=React.DOM&harmony'
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.scss$/,
                loader: "style-loader!css-loader!sass-loader"
            },
            {
                test: /\.(png|jpg)$/,
                loader: 'url-loader?limit=8192'
            },
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.target.mk$/,
                loader: "raw-loader"
            },
            {
                test: /\.node$/,
                loader: "node-loader"
            },
            {
                exclude: /node_modules/,
                test: /.jsx?$/,
                loader: 'babel-loader'
            }
        ]
    },
    node: {
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
    }
};

我使用带有许多 jsx 文件的反应路由器来创建我的网页,我还有一个从数据库中获取数据的 api,mongoose 似乎在那里工作正常,但在任何前端文件上都没有

【问题讨论】:

  • 这是一个电子应用程序吗?如果是使用 react 构建的网页,则无法(而且这是一个坏主意)连接到数据库。
  • 对不起,它是一个网络应用程序
  • 您不想从客户端进行数据库调用。将其保存在您的服务器中。让客户端调用一个 API,然后该 API 将与数据库进行通信。

标签: javascript node.js mongodb mongoose react-jsx


【解决方案1】:

这里的问题是,mongoose 是 MongoDB 之上的一个抽象层,它的依赖项之一是 MongoDB 客户端,其中一些是用本机代码(C、C++)编写的。

你不能用 webpack 构建类似的东西。

【讨论】:

  • mongodb客户端是JS写的
猜你喜欢
  • 2017-06-21
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 2015-09-10
  • 2022-08-13
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多