question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

require with expression not passed to externals function

See original GitHub issue

I use webpack for Node projects and want to require JSON files with Nodes native require.

This works:

  // in order to ignore built-in modules like path, fs, etc.
  target: 'node',
  externals: [
    // require json files with nodes built-in require logic
    function(context, request, callback) {
      if (/\.json$/.test(request)) {
        callback(null, 'commonjs ' + request);
      } else {
        callback();
      }
    },
    // in order to ignore all modules in node_modules folder
    WebpackNodeExternals()
  ],
require('./package.json');

But this doesn’t work:

require(path.join(process.cwd(), 'package.json'));

It seems it isn’t passed to function(context, request, callback). I only get this:

WARNING in ./src/index.ts
Critical dependencies:
44:27-86 the request of a dependency is an expression
 @ ./src/index.ts 44:27-86

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
valleykidcommented, Dec 21, 2016

I also have a use case for this. I think it can be solved like this:

// ./src/require.js
module.exports = require;
// ./src/app.js
import requirex from './require';
...
requirex(path.join(process.cwd(), 'package.json'));
// webpack.config.js
{
     ...
     module: {
          noParse: /require.js$/,
          ...
     }
}

This way worked for me, suggest you have a try. Good luck~

0reactions
webpack-botcommented, Aug 26, 2017

Issue was closed because of inactivity.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is Dynamic require(expression) with webpack not working
the solution is with require.context and webpack . I have created webpackConfig.resolve.alias pointing to my appRoot .
Read more >
Externals - webpack
Specify the default type of externals as 'node-commonjs' . Webpack will import createRequire from 'module' to construct a require function for loading externals...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
What's New In Python 3.8 — Python 3.11.1 documentation
In this example, the assignment expression helps avoid calling len() twice ... For example, the built-in divmod() function does not accept keyword arguments ......
Read more >
13.1.17 CREATE PROCEDURE and CREATE FUNCTION ...
A loadable function can be regarded as an external stored function. ... If you require that expressions passed to a routine be assigned...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found