i18next / i18next-express-middleware

[deprecated] can be replaced with i18next-http-middleware
https://github.com/i18next/i18next-http-middleware
MIT License
206 stars 52 forks source link

TypeError: Cannot read property 'type' of undefined #133

Closed andycaramba closed 7 years ago

andycaramba commented 7 years ago

Hi. I've got this error with i18next-express-middleware

node_modules/i18next/dist/commonjs/i18next.js:271
    if (module.type === 'backend') {
              ^

TypeError: Cannot read property 'type' of undefined
    at I18n.use (node_modules/i18next/dist/commonjs/i18next.js:271:15)
    at Object.<anonymous> (src/server/i18n.js:8:4)
    at Module._compile (module.js:569:30)
    at loader (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (src/server/services/authentication/index.js:6:1)
    at Module._compile (module.js:569:30)
    at loader (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel-cli/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)

I use webpack and babel-node to start my app (babel-node --harmony src/server)

src/server/i18n.js content

import i18next from 'i18next';
import middleware from 'i18next-express-middleware';

import en from '../i18n/en';
import ru from '../i18n/ru';

i18next
  .use(middleware.LanguageDetector) // <-- without this string all works fine
  .init({
    lng: 'en',
    fallbackLng: 'en',
    resources: {
      en,
      ru,
    },
  });

export default i18next;

src/server/services/authentication/index.jscontent (I use feathersjs as server framework)

import auth from 'feathers-authentication';
import local from 'feathers-authentication-local';
import jwt from 'feathers-authentication-jwt';

import i18next from '../../i18n';

export default function () {
  const app = this;
  const config = app.get('auth');

  app.configure(auth(config))
    .configure(jwt())
    .configure(local());

  app.service('authentication').hooks({
    error: {
      create: [
        (hook) => {
          if (hook.error.name === 'NotAuthenticated') {
            hook.error.message = i18next.t('auth.invalidLogin');
          }
        },
      ],
    },
  });
}

my webpack.server.config.js content

const webpack = require('webpack');
const merge = require('webpack-merge');
const base = require('./webpack.base.config');

module.exports = merge(base, {
  target: 'node',
  devtool: '#source-map',
  entry: './src/app/entry-server.js',
  output: {
    filename: 'server-bundle.js',
    libraryTarget: 'commonjs2',
  },
  externals: Object.keys(require('../package.json').dependencies),
  plugins: [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
      'process.env.VUE_ENV': '"server"',
    }),
  ],
});
jamuhl commented 7 years ago

rather sure you can't yet use import statement for i18next-express-middleware on node.js it would use the commonjs build -> import should be const middleware = require('i18next-express-middleware');

or importing: import { default as middleware } from 'i18next-express-middleware';

andycaramba commented 7 years ago

import { default as middleware } from 'i18next-express-middleware'; throws the same error but const middleware = require('i18next-express-middleware'); works fine

Thank you.

jamuhl commented 7 years ago

Will need to update this module at some point to better support the import statements.

henryhuang commented 6 years ago

it works for me:

import i18nMiddleware, { LanguageDetector } from 'i18next-express-middleware';

saeta-eth commented 6 years ago

i18nextMiddleware has the following methods:

i18nextMiddleware { 
   handle: [Function: handle],
   getResourcesHandler: [Function: getResourcesHandler],
   missingKeyHandler: [Function: missingKeyHandler],
   addRoute: [Function: addRoute] 
}

As said @henryhuang, the import statement shoud beimport i18nMiddleware, { LanguageDetector } from 'i18next-express-middleware';

supukarmin commented 6 years ago
import * as i18nextMiddleware from 'i18next-express-middleware';
console.log(i18nextMiddleware);
// outputs:
{ LanguageDetector: { [Function: LanguageDetector] type: 'languageDetector' },
  handle: [Function: handle],
  getResourcesHandler: [Function: getResourcesHandler],
  missingKeyHandler: [Function: missingKeyHandler],
  addRoute: [Function: addRoute],
  default: 
   { handle: [Function: handle],
     getResourcesHandler: [Function: getResourcesHandler],
     missingKeyHandler: [Function: missingKeyHandler],
     addRoute: [Function: addRoute] } }

This should be reopened and there should be made a fix to include also LanguageDetector (like everything else) in the default export. I'm sure there will be much more people wasting their time on this.