Skip to content

typeof __non_webpack_require__ is not known during bundle #5939

Closed

Description

@RoboPhred

Do you want to request a feature or report a bug?
bug

What is the current behavior?
typeof __non_webpack_require__ is not resolved, resulting in webpack considering this a runtime check and breaking its usage as a guard to choose which form of the require func to use.

If the current behavior is a bug, please provide the steps to reproduce.

The code:

var requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : require;
var targetExports = requireFunc(name);

resolves to:

var requireFunc = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : require;
var targetExports = !(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }());

with webpack config

const LibraryScopeRegex = /^\@bl-libraries\/(.+)$/;
module.exports = {
    target: "node",
    entry: path.resolve(pkg.main),
    output: {
        path: dest(),
        filename: `./${libraryName}.js`,
        library: libraryName,
        libraryTarget: "commonjs2"
    },

    resolve: {
        extensions: [".js", ".json"]
    },

    externals: [
        function(context, request, callback) {
            // Anything with this scope will be external and scopeless in the runtime env.
            const match = LibraryScopeRegex.exec(request);
            if (match) {
                return callback(null, 'commonjs2 ' + match[1]);
            }

            // Everything else should be bundled
            callback();
        }
    ],

    plugins: [
        new webpack.NamedModulesPlugin()
    ]
}

I am attempting to make a library that will both function pre- and post- webpack with a nodejs target on both sides.
This module needs to forward a require statement to node, so it should get the top-level nodejs module require() func both from the original source and from the webpack output.

However, webpack seems not to be aware of the type of __non_webpack_require__, and tries to resolve the usage of requireFunc as a webpack-level require().

What is the expected behavior?

I expected webpack to identify __non_webpack_require__ as one of its own, resolve it in setting requireFunc, and to leave requireFunc alone after that.

I am able to get the desired results by changing REPLACEMENT_TYPES at lib/APIPlugin.js:21

const REPLACEMENT_TYPES = {
	__webpack_public_path__: "string", // eslint-disable-line camelcase
	__webpack_require__: "function", // eslint-disable-line camelcase
	__webpack_modules__: "object", // eslint-disable-line camelcase
	__webpack_chunk_load__: "function", // eslint-disable-line camelcase
	__webpack_nonce__: "string" // eslint-disable-line camelcase

        // FIX: Teach webpack about the type of  __non_webpack_require__
        __non_webpack_require__ : "function"
};

With this fix in place, the output code becomes

var requireFunc =  true ? require : require;
var targetExports = requireFunc(name);

and functions the way I expect it to.

As a workaround, I am checking the existence of webpack using typeof __webpack_require__, which results in the same code as the proposed fix.
Am I on-target here? Is this the appropriate way to handle my use case?

If this is a feature request, what is motivation or use case for changing the behavior?

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Node 8.9.0
Webpack 3.8.1
Windows 10

Activity

  1. changed the title [-]typeof __non_webpack_require__ is not resolved[/-] [+]typeof __non_webpack_require__ is not known during bundle[/+] on Nov 8, 2017
  2. webpack-bot commented on May 23, 2018

    @webpack-bot
    Contributor

    This issue had no activity for at least half a year.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  3. added
    status: stale
    on May 23, 2018
  4. webpack-bot commented on Jun 7, 2018

    @webpack-bot
    Contributor

    Issue was closed because of inactivity.

    If you think this is still a valid issue, please file a new issue with additional information.

  5. AshaParveenShaik commented on May 11, 2022

    @AshaParveenShaik

    operations-api\node_modules\bindings\bindings.js:135
    throw err;
    ^
    Error: Could not locate the bindings file. Tried:

    Getting this error someone please help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: stale

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions