smrq / babel-plugin-require-context-hook

Babel plugin to replicate Webpack require.context
47 stars 14 forks source link

__requireContext is not defined #12

Open antrannguyen opened 3 years ago

antrannguyen commented 3 years ago

Getting this error, when it's looking for the process.env.NODE_ENV before the require.context is called in the models index.js file

if (process.env.NODE_ENV === 'development') { require('babel-plugin-require-context-hook/register')(); }

export default (sequelize) => { let db = {};

const context = require.context('.', true, /^./(?!index.js).*.js$/, 'sync');

context .keys() .map(context) .forEach((module) => { const model = module(sequelize, Sequelize); db[model.name] = model; });

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

return db; };

markhorgan commented 3 years ago

process.env.NODE_ENV might be undefined and require('babel-plugin-require-context-hook/register')() might not be called. Try adding console.log(process.env.NODE_ENV) above it.

You might need to set NODE_ENV in package.json:

scripts: {
  "server": "set NODE_ENV=development && nodemon --exec babel-node --watch src/server src/server/index.js"
}
ankurkapil8 commented 3 years ago

@markhorgan

This plugin is not working for me.Below is my code. I am using jest.

require('babel-plugin-require-context-hook/register')(); const context = require.context('./', false, /.ts|.js$/); const keys = context .keys() .filter(item => item.indexOf('d.ts') === -1 && item !== './index.ts' && item !== './index.js');

const models = [];

for (const key of keys) { models.push(context(key)); } export default models;