javascript - Sequelize: cannot find module (Model) - Stack Overflow

I have a Node environment with Sequelize and Webpack on.This is the path where Webpack generates my bui

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.

Share Improve this question asked Jan 28, 2018 at 11:06 SyncroITSyncroIT 1,5781 gold badge15 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I 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

相关推荐

  • javascript - Sequelize: cannot find module (Model) - Stack Overflow

    I have a Node environment with Sequelize and Webpack on.This is the path where Webpack generates my bui

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信