Joi: Cannot resolve module 'net'

Created on 2 Jun 2015  路  24Comments  路  Source: sideway/joi

Here you are requiring the module 'net':
https://github.com/hapijs/joi/blob/master/lib/string.js#L3

But there is not 'net' dependency in the package.json:
https://github.com/hapijs/joi/blob/master/package.json

And at build time, it throws
Module not found: Error: Cannot resolve module 'net' on node_modules/joi/lib @ ./~/joi/lib/string.js 3:10-24

Am I doing something wrong?

Thank you!

non issue

Most helpful comment

@javierbyte I'm not sure about gulp but in webpack it's easy to stub the modules Joi depends on using the nodes field in the webpack.config. E.g.

    node: {
      net: 'empty',
      tls: 'empty',
      dns: 'empty'
    }

All 24 comments

net is a core module. It's build into the node binary, so you don't need to add it to the package.json.

Build time? There is no such thing in joi. Probably something wrong with your environment.

I'm guessing this is referring to compiling Joi to be used client side. In that case there is a build time.

That's even less my concern then.

Exactly @rbhalla, is there a way to use Joi in the client side? I'm using gulp + babelify.

@javierbyte I'm not sure about gulp but in webpack it's easy to stub the modules Joi depends on using the nodes field in the webpack.config. E.g.

    node: {
      net: 'empty',
      tls: 'empty',
      dns: 'empty'
    }

thanks @Rodeoclash that solved the issue for me.

That helped me too. FWIW, it's also possible to use Joi in React with react-validation-mixin.

unfortunately we seem to be further away from Universal Joi

missing modules now

  • net
  • tls
  • dgram
  • fs
  • module
  • child_process

I wonder if at this point it's worth stating up front in the docs that this lib will not work in the browser.

I think i mentioned it somewhere else but https://github.com/jquense/yup is a _not quite drop-in replacement_ that works well in the browser and follows a similar pattern+rules.

Even when you stub out those modules, joi can still add ~3mb to your javascript assets which is a huge performance issue on the frontend.

yeah, I found it thanks :) The tldr is though, for the browser, Joi aint happening. And thats fine - its just I think it's good to be explicit about it.

Until bundlers figure out a proper way to deal with it yeah, I'm not putting any effort into that, but you're free to help if you think you have a decent way to do it.

Same issue here, no luck on adding the node config :(

ERROR in ./~/joi/lib/types/string/index.js
Module not found: Error: Cannot resolve module 'net' in /opt/js/test/node_modules/joi/lib/types/string
 @ ./~/joi/lib/types/string/index.js 5:12-26

ERROR in ./~/isemail/lib/index.js
Module not found: Error: Cannot resolve module 'dns' in /opt/js/test/node_modules/isemail/lib
 @ ./~/isemail/lib/index.js 5:12-26
  node: {
    net: 'empty',
    dns: 'empty'
  },

I have this problem when I tried to use jsonwebtoken model in my project. My guessing is there is some conflict with it. I tried to use another model and it works.

ERROR in ./~/joi/lib/types/string/index.js
Module not found: Error: Cannot resolve module 'net' in /opt/js/test/node_modules/joi/lib/types/string
 @ ./~/joi/lib/types/string/index.js 5:12-26

ERROR in ./~/isemail/lib/index.js
Module not found: Error: Cannot resolve module 'dns' in /opt/js/test/node_modules/isemail/lib
 @ ./~/isemail/lib/index.js 5:12-26

@aadshalshihry, me too. Thanks, I didn't know jsonwebtoken was the cause.

@Marsup, Joi is awesome and I'm really grateful for it, I think it's the first reliable validation library I've (happily) used, full featured, really nice API, I'm just wondering why it couldn't be used client side? And why you see no point in it? That was what led me to using it in the first place, having same validation on both backend/frontend.
I can easily understand it's difficult to maintain a library for various environments, but speaking of javascript, that's one of its undeniable strengh.
A lot of tools/frameworks/libraries are implementing SSR and such, it could be really nice to be able to use Joi with those.

Would you be opened to have joi supporting both on the main repo? Having 2 distinct packages? I'll be happy to help if you're ok with that

I don't think I ever said joi couldn't be used client-side, even though it's slightly fat for a front-end dependency. I'm open to it but not in the usual way other modules do it, and so far I haven't found any satisfactory approach. I have a lead but it requires an investment in time I can't provide right now.

Sorry, I mean you didn't said that, but it cannot for now (or didn't found the right configuration to do so)

There is a better solution available here

I having the same problem to use with Angular.

Go this problem also. Solved by @Rodeoclash

Seems like a nice package, would be good to get this sorted.

I had same problem in Next.js and solve by reffering Rodeoclash's code, Thx 鈽猴笍

// next.config.js
const withCss = require('@zeit/next-css');
module.exports = withCss({
  webpack (config, options) {
    config.module.rules.push({
      test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
      use: {
        loader: 'url-loader',
      }
    });
    config.node = {
      ...(config.node || {}),
      net: 'empty',
      tls: 'empty',
      dns: 'empty'
    };
    return config;
  }
});

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings