Node modules resolution via Babel + Webpack: Babel compiler errors

I’m trying to resolve a node module to be able to create webrtc connection needed for some of our tests based on the pattern and config presented here:

Webpack config:

module.exports = {
	mode: 'production',
	output: {
		path: path.join(__dirname, 'dist'),
		libraryTarget: 'commonjs',
		filename: '[name].js',
	},
	entry: entry(entry.basePath('scripts'), './scripts/**/*.js'),
	resolve: {
		fallback: {
			https: require.resolve('https-browserify'),
			"url": require.resolve("url/"),
			"http": require.resolve("stream-http"),
			"buffer": require.resolve("buffer/")
		},
		extensions: ['.ts', '.tsx', '.js'],
	},
	module: {
		rules: [
			{
				test: /\.js$/,
				loader: "babel-loader",
			},
		],
	},
	externals: /^(k6|https?\:\/\/)(\/.*)?/,
	stats: {
		colors: true,
	},
	plugins: [
		new CleanWebpackPlugin(),
		new CopyPlugin({
			patterns: [
				{
					context: './lib',
					from: './fake-data/*.json',
				},
			],
		}),
	],

It works with presented node packages, but with the one I work with it fails on the babel compilation stage with error like

k6-load-tests/dist/test.js: Unexpected token, expected ( (2:175532)

                                  ^
        at <internal/k6/compiler/lib/babel.min.js>:7:10099(24)
        at <internal/k6/compiler/lib/babel.min.js>:5:29529(56)
        at <internal/k6/compiler/lib/babel.min.js>:7:8385(51)
        at <internal/k6/compiler/lib/babel.min.js>:7:5838(57)
        at <internal/k6/compiler/lib/babel.min.js>:7:3622(281)
        at <internal/k6/compiler/lib/babel.min.js>:6:30266(598)
        at <internal/k6/compiler/lib/babel.min.js>:6:25697(16)
        at <internal/k6/compiler/lib/babel.min.js>:6:25286(124)
        at <internal/k6/compiler/lib/babel.min.js>:6:23854(12)
        at <internal/k6/compiler/lib/babel.min.js>:6:23442(13)
        at <internal/k6/compiler/lib/babel.min.js>:6:22671(73)
        at <internal/k6/compiler/lib/babel.min.js>:6:5488(27)
        at <internal/k6/compiler/lib/babel.min.js>:6:2562(11)
        at <internal/k6/compiler/lib/babel.min.js>:5:31032(259)
        at <internal/k6/compiler/lib/babel.min.js>:6:4439(38)
        at <internal/k6/compiler/lib/babel.min.js>:6:4049(19)
        at <internal/k6/compiler/lib/babel.min.js>:7:7060(62)
        at <internal/k6/compiler/lib/babel.min.js>:7:6374(21)
        at <internal/k6/compiler/lib/babel.min.js>:7:613(225)
        at <internal/k6/compiler/lib/babel.min.js>:6:30046(561)
        at <internal/k6/compiler/lib/babel.min.js>:6:25697(16)
        at <internal/k6/compiler/lib/babel.min.js>:6:25286(124)
        at <internal/k6/compiler/lib/babel.min.js>:6:23854(12)
        at <internal/k6/compiler/lib/babel.min.js>:6:23442(13)
        at <internal/k6/compiler/lib/babel.min.js>:6:22671(73)
        at <internal/k6/compiler/lib/babel.min.js>:7:5011(25)
        at <internal/k6/compiler/lib/babel.min.js>:7:5503(16)
        at <internal/k6/compiler/lib/babel.min.js>:7:3648(292)
        at <internal/k6/compiler/lib/babel.min.js>:6:30266(598)
        at <internal/k6/compiler/lib/babel.min.js>:6:25697(16)
        at <internal/k6/compiler/lib/babel.min.js>:6:25286(124)
        at <internal/k6/compiler/lib/babel.min.js>:6:23854(12)
        at <internal/k6/compiler/lib/babel.min.js>:6:23442(13)
        at <internal/k6/compiler/lib/babel.min.js>:6:22671(73)
        at <internal/k6/compiler/lib/babel.min.js>:6:5488(27)
        at <internal/k6/compiler/lib/babel.min.js>:6:2562(11)
        at <internal/k6/compiler/lib/babel.min.js>:5:31032(259)
        at <internal/k6/compiler/lib/babel.min.js>:6:4439(38)
        at <internal/k6/compiler/lib/babel.min.js>:6:4049(19)
        at <internal/k6/compiler/lib/babel.min.js>:7:7060(62)
        at <internal/k6/compiler/lib/babel.min.js>:7:6374(21)
        at <internal/k6/compiler/lib/babel.min.js>:7:613(225)
        at <internal/k6/compiler/lib/babel.min.js>:6:30046(561)
        at <internal/k6/compiler/lib/babel.min.js>:6:25697(16)
        at <internal/k6/compiler/lib/babel.min.js>:6:25286(124)
        at <internal/k6/compiler/lib/babel.min.js>:6:23854(12)
        at <internal/k6/compiler/lib/babel.min.js>:6:23442(13)
        at <internal/k6/compiler/lib/babel.min.js>:6:22671(73)
        at <internal/k6/compiler/lib/babel.min.js>:7:176(119)
        at <internal/k6/compiler/lib/babel.min.js>:6:30046(561)
        at <internal/k6/compiler/lib/babel.min.js>:6:25697(16)
        at <internal/k6/compiler/lib/babel.min.js>:6:25286(124)
        at <internal/k6/compiler/lib/babel.min.js>:6:23854(12)
        at <internal/k6/compiler/lib/babel.min.js>:6:23442(13)
        at <internal/k6/compiler/lib/babel.min.js>:6:22671(73)
        at <internal/k6/compiler/lib/babel.min.js>:6:22078(13)
        at <internal/k6/compiler/lib/babel.min.js>:5:31890(386)
        at <internal/k6/compiler/lib/babel.min.js>:6:4439(38)
        at <internal/k6/compiler/lib/babel.min.js>:5:29672(15)
        at <internal/k6/compiler/lib/babel.min.js>:5:28444(17)
        at h (<internal/k6/compiler/lib/babel.min.js>:4:30563(6))
        at <internal/k6/compiler/lib/babel.min.js>:2:27418(93)
        at <internal/k6/compiler/lib/babel.min.js>:2:28703(9)
        at <internal/k6/compiler/lib/babel.min.js>:14:24458(9)
        at <internal/k6/compiler/lib/babel.min.js>:2:28270(21)
        at <internal/k6/compiler/lib/babel.min.js>:14:24413(11)
        at bound  (native)
        at s (<internal/k6/compiler/lib/babel.min.js>:1:1327(8))  hint="script exception"

The char where the error message points to is ‘=’ var ye={...Object.freeze({__proto__:null,hasBrowserEnv:le,hasStandardBrowserWebWorkerEnv:pe,hasStandardBrowserEnv:fe,origin:de})

Any idea where to look at the first place? Is it webpack \ babel issue? Some pluggins are needed maybe?

Hi @dudefrombelarus,

Please try to use the latest k6 version v0.53.0 - released earlier this week.

It does dorp the internall babel, that is giving you the current error.

I don’t know if it is going to resolev this, but at least we won’t be trying to fix it on an older version.

After updating I get only this error
ERRO[0000] GoError: file:/dist/test.js: Line 2:175735 Unexpected token await (and 15 more errors) hint=“script exception”

It points to

for await(const n of e)yield*qe(ArrayBuffer.isView(n)?n:await r(String(n)),t)

This seems like async iterator which isn’t supported by k6.

I can only expect that one of hte modules you add use this.

Looking at

			https: require.resolve('https-browserify'),
			"url": require.resolve("url/"),
			"http": require.resolve("stream-http"),
			"buffer": require.resolve("buffer/")

I would expect that stream-http will be the one … and also I would expect it will not work in any way as it will have to do network calls and I doubt it was written to work with k6.

1 Like

Thank you for your response.

I’ve removed the modules that require all the mentioned libs. Now I only tried to to resolve @roamhq/wrtc - npm in this way to be able to create webrtc connection.
I got this error:

 ReferenceError: window is not defined

From what I get, nothing that refers to window can be run in context of k6 and webpack + babel won’t solve that? Are there any known alternatives to create webrtc connection in k6 or any plans to implement such functionality in k6 itself?

From what I get, nothing that refers to window can be run in context of k6

I guess you can use a similar trick to this one to realias window to globalThis . But I doubt this will get you a bit more further.

As I said before

and also I would expect it will not work in any way as it will have to do network calls and I doubt it was written to work with k6.

I don’t know much about webrtc. You might be able to go with k6 browser but that runs a full chromium browser, so you might have problem scaling up.

Apart from that you might be able to create a k6 extension use that. How complex that is depends on how much you know go, and … well how complex stuff you may want to do.

1 Like