webpack无法捆绑sequelize

时间:2017-05-16 08:47:33

标签: express typescript npm webpack

我目前正在使用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
    })
  ])
}

Fyi,在我将Sequelize添加到我的项目之前,它只使用Express +打字稿。我知道有些东西我不知道因为我还是webpack环境的新手。感谢。

1 个答案:

答案 0 :(得分:0)

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

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

希望这会对你有所帮助。