node.js - Webpack bundling doesn't include specific module such as 'sequelize' -
i'm developing nodejs webpack, seqelize , other modules below webpack configuration.
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` , `.tsx` 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' } }
in case, try bundle whole sources config , bundled file named server.[chunkhash].js
.
i want move file server , make work command node server.[chuckhash].js
, however, got message below.
module.js:472 throw err; ^ error: cannot find module 'sequelize' @ function.module._resolvefilename (module.js:470:15) @ function.module._load (module.js:418:25) @ module.require (module.js:498:17) @ require (internal/module.js:20:19) ...
so, tried find specific point make error, , found out models/index.js
use seqelize
module below codes.
import fs 'fs'; import path 'path'; import sequelize 'sequelize'; import config '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;
how fix issue?
actually, nodemodule in same folder, there has been no error, but, make bundled file, make error.
you defined node_modules
externals (externals: nodemodules,
). means webpack won't bundle module comes node_modules
, leave imports resolved @ runtime, when running in node without using webpack. work need have modules available wherever run bundle.
if want webpack bundle node_modules
well, can remove externals
option.
the externals config you're using came (directly or indirectly) backend apps webpack (part i) , should read blog post understand it's doing , whether need it.
Comments
Post a Comment