Import @hapi/joi in Worker cause error

Hi, i try to import @hapi/joi like this:

import Joi from '@hapi/joi';

it build successfully. but in preview, it return error:

Error: Error("expected value", line: 2, column: 1)

If i remove import Joi part, everything goes well. Can you help to fixed this?

Cloudflare Workers isn’t a Node.js environment, so unless you are packaging it with something like webpack the import won’t work. I presume that is the issue here.

1 Like

I did package as webpack. I use different package actually:

import {config, Endpoint, S3} from 'aws-sdk';
import moment from 'moment';
import Joi from '@hapi/joi';

everything working fine, except if i import @hapi/joi.

this is my webpack.config.js:

module.exports = {
  entry: __dirname + '/index.js',
  mode: 'production',
  optimization: {
    minimize: false,
  },
  performance: {
    hints: false,
  },
  devtool: 'cheap-module-source-map',
  node: {
    net: 'empty',
    tls: 'empty',
    dns: 'empty',
  },
  output: {
    path: __dirname + '/dist',
    publicPath: 'dist',
    filename: 'worker.js',
  },
};

Well, then you basically replied to yourself…

Unless there is a specific issue with that package which only the team knows (cc @KentonVarda @signalnerve) or someone else, the fact that some packages work makes this issue not really a Worker’s one so this may not be the best place (Stack Overflow, maybe?).

Also: what error does it give you?

That is the error i get. I attach preview from my console:

But… the error is in line 2. That’s strange.

Also, have you actually tried putting it in exactly as they say const Joi = require('@hapi/joi');? It shouldn’t matter, but you might never know.

Two things I can recommend there:

  1. open an issue in the wrangler CLI GitHub report so that Ashley (who I believe follows the development of that can read it and check on it)
  2. maybe @cloonan or @sklabnik can ping Ashley and let her see this conversation and maybe make her open a Community account :slight_smile: I could try to ping her on Twitter, but they are the best bet since I don’t know how much she will reply to a random tweet even if we met once, maybe I’ll try :rofl:
2 Likes

Done. Submit new issue https://github.com/cloudflare/wrangler/issues/393
Thank you for your help @matteo

2 Likes