Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Webpack loads unwanted file when passing string+variable to require()

$
0
0

When running the following code I get an error when building with Webpack:

const name = 'Test';
const test = require(`./app/model/${name}`);

However, with the following code I get no error:

const test = require('./app/model/Test');

The error received is:

ERROR in ./node_modules/express/lib/request.js
Module not found: Error: Can't resolve 'net' in '/path/to/maestro/node_modules/express/lib'
 @ ./node_modules/express/lib/request.js 18:11-25
 @ ./node_modules/express/lib/express.js
 @ ./node_modules/express/index.js
 @ ./app/model/CommanderProgram.js
 @ ./app/model sync ^\.\/.*$
 @ ./web.js
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! xops-pat@4.0.0-rc1 build: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the xops-pat@4.0.0-rc1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /path/to/error/log/2019-12-09T16_19_45_494Z-debug.log

The contents of ./app/model/Test.js are:

'use strict';
module.exports = 'asdf';

So no dependencies. The error above mentions ./app/model/CommanderProgram.js, which absolutely should not be Webpack'd. Based on the fact that Test.js has nothing in it, Webpack should not be trying to include CommanderProgram.js, though. However, if I delete CommanderProgram.js then the error does NOT occur. Restoring CommanderProgram.js then renaming it to either AAA.js or ZZZ.js still DOES cause the problem (without updating references, so nothing could be importing AAA.js/ZZZ.js), so somehow Webpack is trying to include an un-referenced file.

I also tried the following, showing it's not an issue with template literals:

const name = 'Test';
const test = require('./app/model/' + name);

Why does Webpack appear to include an un-referenced file when using variables in strings passed to require?

Environment:

  • Node: v10.16.3
  • OS: Windows 10
  • Webpack: 4.41.2
  • Webpack-cli: 3.3.10

See code here: https://github.com/xOPERATIONS/maestro/blob/9d7977f52c1a08bc947666f3b1c85c30787cb831/web.js


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>