I have a Node environment with Sequelize and Webpack on.
This is the path where Webpack generates my build:
./bin/builds.js
This one is the path where I genereted my models by using Sequelize CLI:
./src/models
In this folder there are my models and an index file containing a script generated by Sequelize. This script gets all the models and exports them, but in my case it's not working.
This is the error:
Error: Cannot find module '/Library/WebServer/Documents/private/my-project/bin/src/models/currency.js'
The script tries to fetch the models in the build path (/bin/
) while the models are in /my-project/src
.
I fixed that by just editing the line below inside index.js:
var model = sequelize['import'](path.join(__dirname, file));
Into:
var model = sequelize['import'](path.join('../'+__dirname, file));
But I want to find a better way to fix it, because by fixing the index.js I think that bug will happen again on other files.
This is my webpack configuration:
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const nodeModules = {};
fs.readdirSync(path.resolve(__dirname, 'node_modules'))
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'monjs ' + mod;
});
module.exports =
{
name: 'server',
target: 'node',
entry: './app.js',
output: {
path: __dirname+'/bin/',
publicPath: '/',
filename: 'server.js'
},
node: {
__filename: true,
__dirname: true
},
externals: [nodeModules],
module: {
loaders: [
{ test: /\.js$/,
loaders: [
'babel-loader'
]
},
{ test: /\.json$/, loader: 'json-loader' },
]
}
};
NB: I have already tried to replace publicPath into '/'
but it doesn't work.
I have a Node environment with Sequelize and Webpack on.
This is the path where Webpack generates my build:
./bin/builds.js
This one is the path where I genereted my models by using Sequelize CLI:
./src/models
In this folder there are my models and an index file containing a script generated by Sequelize. This script gets all the models and exports them, but in my case it's not working.
This is the error:
Error: Cannot find module '/Library/WebServer/Documents/private/my-project/bin/src/models/currency.js'
The script tries to fetch the models in the build path (/bin/
) while the models are in /my-project/src
.
I fixed that by just editing the line below inside index.js:
var model = sequelize['import'](path.join(__dirname, file));
Into:
var model = sequelize['import'](path.join('../'+__dirname, file));
But I want to find a better way to fix it, because by fixing the index.js I think that bug will happen again on other files.
This is my webpack configuration:
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const nodeModules = {};
fs.readdirSync(path.resolve(__dirname, 'node_modules'))
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'monjs ' + mod;
});
module.exports =
{
name: 'server',
target: 'node',
entry: './app.js',
output: {
path: __dirname+'/bin/',
publicPath: '/',
filename: 'server.js'
},
node: {
__filename: true,
__dirname: true
},
externals: [nodeModules],
module: {
loaders: [
{ test: /\.js$/,
loaders: [
'babel-loader'
]
},
{ test: /\.json$/, loader: 'json-loader' },
]
}
};
NB: I have already tried to replace publicPath into '/'
but it doesn't work.
1 Answer
Reset to default 5I faced same issue, my solution is manually import models into index.js
'use strict'
var Sequelize = require('sequelize')
var env = process.env.NODE_ENV || 'development'
var config = require(__dirname + '/../config/config.json')[env]
var db = {}
if (config.use_env_variable) {
var sequelize = new Sequelize(process.env[config.use_env_variable], config)
} else {
var sequelize = new Sequelize(config.database, config.username, config.password, config)
}
const models = [
require('./Person')(sequelize, Sequelize),
require('./Animal')(sequelize, Sequelize)
]
models.forEach(model => {
db[model.name] = model
})
models.forEach(model => {
if (db[model.name].associate) {
console.log('entered', model.name)
db[model.name].associate(db)
}
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745371668a4624821.html
评论列表(0条)