Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warnings when building backend Express/WS Node app with Webpack

I am getting some confusing warnings when building a backend Node server with Webpack. I want to use Webpack to build my backend primarily for two reasons:

  • Webpack creates a single executable file, which is easier to deploy
  • Webpack includes all of my app's dependencies, so I can deploy my app to any compatible Node environment without needing to install dependencies first

Here are the warnings I'm getting:

WARNING in ./~/ws/lib/BufferUtil.js
Module not found: Error: Can't resolve 'bufferutil' in .../node_modules/ws/lib
 @ ./~/ws/lib/BufferUtil.js 35:21-42
 @ ./~/ws/lib/Receiver.js
 @ ./~/ws/index.js
 @ ./src/main.js

WARNING in ./~/ws/lib/Validation.js
Module not found: Error: Can't resolve 'utf-8-validate' in .../node_modules/ws/lib
 @ ./~/ws/lib/Validation.js 10:22-47
 @ ./~/ws/lib/Receiver.js
 @ ./~/ws/index.js
 @ ./src/main.js

WARNING in ./~/express/lib/view.js
80:29-41 Critical dependency: the request of a dependency is an expression

For the Critical dependency warning, I've found a good example explaining the problem and some documentation on how to use the ContextReplacementPlugin, although it's still unclear to me how to apply it to this situation. It looks like the warning is being caused by line 80 in node_modules/express/lib/view.js:

opts.engines[this.ext] = require(mod).__express

It is clear to me that the dependency cannot be resolved at build time, so how can I use the ContextReplacementPlugin to fix this dependency?

As for the Module not found warnings in the ws package, it's unclear to me what's going on. It looks like those dependencies exist in my global node_modules, and maybe they're not being pulled in by Webpack. I've tried adding them to my project's devDependencies, but then I just get Critical dependency warnings for them instead.

My application still runs after being built, so I suppose I could technically ignore the warnings, but I figure that these are widely used Node packages and Webpack is a popular build tool, so there must be a reasonable solution available.


Here are my dependencies in my package.json:

 "devDependencies": {
    "@types/cassandra-driver": "^0.8.10",
    "@types/express": "^4.0.35",
    "@types/uuid": "^2.0.29",
    "@types/ws": "0.0.40",
    "nodemon": "^1.11.0",
    "typescript": "^2.3.1",
    "webpack": "^2.5.1"
  },
  "dependencies": {
    "cassandra-driver": "^3.2.1",
    "express": "^4.15.2",
    "uuid": "^3.0.1",
    "ws": "^2.3.1"
  }

And here's my webpack.config.js:

const path = require('path');

module.exports = {
  entry: './src/main.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'main.js'
  },
  target: 'node',
  node: {
    __dirname: false,
    __filename: false
  }
};

I like keeping things minimal if possible. Thanks for reading.

like image 404
David Kaczynski Avatar asked May 19 '17 13:05

David Kaczynski


1 Answers

The short answer

webpack can work with node, but it cannot extract follow require() statements. Modifications have to be made to ignore require() in order for it to work.

The long answer

It is actually possible to pull some files into a master file and run it in some instances.

  • One instance is if all the modules required are written in typescript and the modules are written in a away that the typescript plugin can parse the module in.

  • Another instance would be if you are using es6 babel plugins and using es6 style imports.

Even in the above scenarios the blunder may choose not to pull in certain files

The ultimate answer

It really should not matter too much about trying to perform the long answer because modules are stored in memory at boot and then referenced later from the cache. See the article below for more information.

http://fredkschott.com/post/2014/06/require-and-the-module-system/

like image 129
thomasmeadows Avatar answered Nov 05 '22 17:11

thomasmeadows