2017-04-07 94 views
0

我正在开发带有webpack,seqelize和其他模块的nodejs,下面的webpack配置。Webpack捆绑不包括特定的模块,如'sequelize'

const path = require('path'); 
const webpack = require('webpack'); 
const fs = require('fs'); 
const glob = require('glob'); 
const CleanWebpackPlugin = require('clean-webpack-plugin'); 

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

module.exports = { 
    // entry: [ path.resolve(__dirname, 'server.js'), models ], 
    entry: glob.sync(path.resolve(__dirname, 'src/**/*.js')), 
    resolve: { 
    // Add `.ts` and `.tsx` as a resolvable extension. 
    root  : [path.resolve(__dirname, '')], 
    extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js'], 
    modulesDirectories: ['node_modules'] 
    }, 
    module: { 
    loaders: [ 
     { 
     test: /\.js$/, 
     loader: 'babel', 
     exclude: /node_modules/, 
     query: { 
      cacheDirectory: true, 
      presets: ['es2015'] 
     } 
     }, { 
     test: /\.json$/, 
     loader: 'json' 
     } 
    ] 
    }, 
    plugins: [ 
    new CleanWebpackPlugin(['build'], { 
     root: path.resolve(__dirname, ''), 
     verbose: true, 
     dry: false, 
     exclude: [], 
     watch: true 
    }) 
    ], 
    node: { 
    __filename: true, 
    __dirname: true 
    }, 
    target: 'node', 
    externals: nodeModules, 
    output: { 
    path: path.resolve(__dirname, 'build'), 
    filename: 'server.[chunkhash].js', 
    libraryTarget: 'commonjs' 
    } 
} 

在这种情况下,我尝试用捆绑的配置整个源,然后得到一个名为server.[chunkhash].js捆绑的文件。

我想将文件移动到服务器,并使用像node server.[chuckhash].js这样的命令工作,但是,我收到了如下所示的消息。

module.js:472 
    throw err; 
    ^

Error: Cannot find module 'sequelize' 
    at Function.Module._resolveFilename (module.js:470:15) 
    at Function.Module._load (module.js:418:25) 
    at Module.require (module.js:498:17) 
    at require (internal/module.js:20:19) 
    ... 

所以,我试图找到具体的点,使错误,然后发现我models/index.js使用seqelize模块下面的代码。

import fs from 'fs'; 
import path from 'path'; 
import Sequelize from 'sequelize'; 
import config from 'config/env'; 

const sequalize = new Sequelize(config.mysql.database, config.mysql.username, config.mysql.password, config.mysql.params.options); 
const db = {}; 

fs.readdirSync(__dirname) 
    .filter(file => { 
    return (file.indexOf('.') !== 0) && (file !== 'index.js'); 
    }) 
    .forEach(file => { 
    const model = sequalize.import(path.resolve(__dirname, file)); 
    db[model.name] = model; 
    }); 

Object.keys(db).forEach(modelName => { 
    if ('associate' in db[modelName]) { 
    db[modelName].associate(db); 
    } 
}); 

db.sequelize = sequalize; 
db.Sequelize = Sequelize; 

export default db; 

我该如何解决这个问题?

实际上,与同一个文件夹中的nodemodule一样,没有错误,但是,使捆绑文件发生错误。

回答

1

您将所有node_modules定义为externalsexternals: nodeModules,)。这意味着webpack不会绑定来自node_modules的任何模块,只会在运行时让导入问题得到解决,就像在Node中运行时不使用webpack一样。为了这个工作,你需要在你运行软件包的地方使用这些模块。

如果您想要webpack绑定node_modules,您可以删除externals选项。

的配置的外部您正在使用可能来到(直接或间接)从Backend Apps with Webpack (Part I),你应该阅读博客文章来了解它的真正在做什么以及你是否需要它。