Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sajithneyo opened this issue Sep 29, 2021 · 1 comment

Comments

@sajithneyo
Copy link

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?

@saturna
Copy link

saturna commented Oct 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants