【问题标题】:webpack fail to bundle sequelizewebpack 无法捆绑 sequelize
【发布时间】:2017-10-15 05:55:05
【问题描述】:

我目前正在使用 Sequelize ORM 开发 Express js+typescript,并希望使用 Webpack 进行捆绑。问题是我每次尝试捆绑时都会收到警告并失败。

这是我的警告/错误信息:

WARNING in ./~/sequelize/lib/dialects/mysql/connection-manager.js
Module not found: Error: Can't resolve 'mysql' in 'D:\Project\MyMe\node_modules\sequelize\lib\dialects\mysql'
 @ ./~/sequelize/lib/dialects/mysql/connection-manager.js 20:17-33
 @ ./~/sequelize/lib/dialects/mysql/index.js
 @ ./~/sequelize/lib/sequelize.js
 @ ./~/sequelize/index.js
 @ ./src/server/models/index.js
 @ ./src/server/controllers/TopUp.ts
 @ ./src/server/controllers/index.ts
 @ ./src/router/router.ts
 @ ./src/App.ts
 @ ./src/index.ts

WARNING in ./~/express/lib/view.js
80:29-41 Critical dependency: the request of a dependency is an expression

WARNING in ./~/sequelize/lib/sequelize.js
686:60-73 Critical dependency: the request of a dependency is an expression

WARNING in ./~/sequelize/lib/dialects/mysql/connection-manager.js
18:17-60 Critical dependency: the request of a dependency is an expression

WARNING in ./~/sequelize/lib/dialects/mssql/connection-manager.js
18:15-71 Critical dependency: the request of a dependency is an expression

WARNING in ./~/sequelize/lib/dialects/postgres/connection-manager.js
20:14-57 Critical dependency: the request of a dependency is an expression

WARNING in ./~/sequelize/lib/dialects/sqlite/connection-manager.js
22:15-71 Critical dependency: the request of a dependency is an expression

ERROR in ./~/pg/lib/native/index.js
Module not found: Error: Can't resolve 'pg-native' in 'D:\Project\MyMe\node_modules\pg\lib\native'
 @ ./~/pg/lib/native/index.js 9:13-33
 @ ./~/pg/lib/index.js
 @ ./~/sequelize/lib/dialects/postgres/connection-manager.js
 @ ./~/sequelize/lib/dialects/postgres/index.js
 @ ./~/sequelize/lib/sequelize.js
 @ ./~/sequelize/index.js
 @ ./src/server/models/index.js
 @ ./src/server/controllers/TopUp.ts
 @ ./src/server/controllers/index.ts
 @ ./src/router/router.ts
 @ ./src/App.ts
 @ ./src/index.ts

这是我的 package.json:

"devDependencies": {
    "@types/body-parser": "^1.16.3",
    "@types/chai": "^3.5.2",
    "@types/chai-http": "0.0.30",
    "@types/debug": "0.0.29",
    "@types/express": "^4.0.35",
    "@types/mocha": "^2.2.41",
    "@types/morgan": "^1.7.32",
    "@types/node": "^7.0.18",
    "chai": "^3.5.0",
    "chai-http": "^3.0.0",
    "mocha": "^3.3.0",
    "ts-loader": "^2.0.3",
    "ts-node": "^3.0.4",
    "typescript": "^2.3.2",
    "webpack": "^2.5.1",
    "webpack-dev-server": "^2.4.5"
  },
  "dependencies": {
    "body-parser": "^1.17.1",
    "debug": "^2.6.6",
    "express": "^4.15.2",
    "morgan": "^1.8.1",
    "pg": "^6.1.5",
    "pg-hstore": "^2.3.2",
    "sequelize": "^3.30.4"
  }

下面是我的 webpack.config.js

var path = require('path');
var webpack = require('webpack')
var fs = require('fs')

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
    return ['.bin'].indexOf(x) === -1;
  })
  .forEach(function(mod) {
    nodeModules[mod] = 'commonjs ' + mod;
  });

module.exports = {
    entry: './src/index.ts',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        loaders: [
            {
                test: /\.ts?$/,
                loader: 'ts-loader',
                exclude: /node_modules/,
            }
        ]
    },
    resolve: {
        extensions: [".tsx", ".ts", ".js"]
    },
    devServer: {
        port: 3000
    },
    target: 'node'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      minimize: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

仅供参考,在我将 Sequelize 添加到我的项目之前,它的捆绑包仅适用于 Express+typescript。我知道我缺少一些东西,因为我还是 webpack env 的新手。谢谢。

【问题讨论】:

    标签: express typescript npm webpack


    【解决方案1】:

    我认为这是因为 sequalize 是为 nodejs 制作的。并非每个模块都是“兼容的”。所以你不能“只是”把它挂在那里。我从未在此设置中使用过均衡器,但它是有道理的。我发现一篇文章显示了合并它所需的步骤:

    https://www.google.nl/amp/s/scotch.io/amp/tutorials/creating-an-angularjs-application-with-sequelize-part-1

    希望对你有所帮助。

    【讨论】:

    • 您好,感谢您的信息。由于 webpack 作者的解决方案,我设法解决了这个问题。解决方案是忽略 bundle 中的所有 node_modules 或使用像 webpack-node-externals 这样的 npm 包。这是我的问题解决了webpack#4879
    猜你喜欢
    • 2018-05-24
    • 2019-01-02
    • 1970-01-01
    • 2017-09-03
    • 2017-08-04
    • 2016-10-05
    • 2020-11-10
    • 1970-01-01
    • 2016-10-07
    相关资源
    最近更新 更多