xregexp icon indicating copy to clipboard operation
xregexp copied to clipboard

"TypeError: XRegExp is not a function" thrown when bundled with Webpack

Open sajithneyo opened this issue 4 years ago • 1 comments
trafficstars

Hey guys,

I have a Node application running in AWS Lambda. For the local development, I use the serverless framework and test locally. But today I faced a strange issue. When deploying to Lambda, I first run it through Webpack to bundle. So it does not work when lambda is invoked but works locally.

So as my local setup is not going through Webpack I uploaded with node modules to the lambda env to be sure if webpack is the one causing the issue. And yes, it's working fine when not ran through webpack to bundle.

So I created a small script

const XRegExp = require("xregexp");

const pattern = XRegExp(
    "(?<source>[\\w:\\.\\-]+)\\s*(?:\\[\\s*(?<keys>.*)\\s*\\])?\\s*->\\s*(?<destination>[\\w:\\.\\-]+)"
);
const match = XRegExp.exec(
    "dead-letters-exchange[routing] -> transcript-dead-letters-queue",
    pattern
);
console.log(match);

I ran this normally through the node command. Gives an output without an error.

Then bundled through webpack and ran the output with the node command. Errors out saying TypeError: XRegExp is not a function

Here is my simple webpack config

const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'index.js',
        libraryTarget: 'commonjs',
    },
    optimization: {
        minimize: false,
    },
    devtool: 'source-map',
    target: 'node',
    mode: 'production',
    module: {},
    externals: ['eslint', 'aws-sdk'],
};

I found that changing target and libraryTarget combinations works. But this is not the case for a huge project base that gets bundled together. So why XRegExp is behaving like this?

sajithneyo avatar Sep 29 '21 13:09 sajithneyo

For anyone with the same problem,using require("xregexp/xregexp-all") solves the issue.

saturna avatar Oct 20 '22 17:10 saturna