0

I have the following webpack.config.js

const path = require('path');
const project = require(path.join(process.cwd(), 'project.json'));
const nodeExternals = require('webpack-node-externals');
const webpack = require('webpack');

const locales = Object.keys(project.locales);

const config = {
entry: {
  server: './server.ts'
},
resolve: {
  extensions: ['.ts', '.js'],
  alias: {}
},
target: 'node',
externals: [nodeExternals({
  whitelist: [
     /^@ng-bootstrap/,
     /dropzone/,
     /dropzone\/dist/,
     /^ngx-dropzone-wrapper/,
     /^ngx-dropzone-wrapper\/dist/,
     /^@agm\/core/
  ]
})],
output: {
  path: path.join(__dirname, 'dist'),
  filename: '[name].js'
},
module: {
  rules: [{
     test: /\.ts$/,
     loader: 'ts-loader'
  }]
},
plugins: [
  // ...
  new webpack.DefinePlugin({
     'process.env.BROWSER': JSON.stringify(true),
     'window': JSON.stringify(true),
     'document': JSON.stringify(true),
     'localStorage': {
        getItem: function () {
           return true
        },
        setItem: function () {
           return true
        },
     }
  }),
  // ...
]
};

for (let i = 0; i < locales.length; i++) {
  config.resolve.alias[`main.server.${locales[i].toLowerCase()}`] = path.join(__dirname, 'dist', 'server', locales[i].toLowerCase(), 'main.js')
}

module.exports = config;

and I'm getting this error when I'm trying to run

node ./dist/server.js

Error:

/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1
(function (exports, require, module, __filename, __dirname) { !function(n){var l={};function e(t){if(l[t])return l[t].exports;var o=l[t]={i:t,l:!1,exports:{}};return n[t].call(o.exports,o,o.exports,e),o.l=!0,o.exports}e.m=n,e.c=l,e.d=function(n,l,t){e.o(n,l)||Object.defineProperty(n,l,{configurable:!1,enumerable:!0,get:t})},e.r=function(n){Object.defineProperty(n,"__esModule",{value:!0})},e.n=function(n){var l=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(l,"a",l),l},e.o=function(n,l){return Object.prototype.hasOwnProperty.call(n,l)},e.p="",e(e.s=108)}([function(n,l){n.exports=require("@angular/core")},function(n,l,e){"use strict";function t(n){return parseInt(""+n,10)}function o(n){return void 0!==n&&null!==n?""+n:""}function u(n,l,e){return void 0===e&&(e=0),Math.max(Math.min(n,l),e)}function r(n){return"string"==typeof n}function a(n){return!isNaN(t(n))}function i(n){return"number"==typeof n&&isFinite(n)&&Math.floo

TypeError: Cannot read property 'documentElement' of undefined
at /home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1:227894
at Object.<anonymous> (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1:228489)
at Object.<anonymous> (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1:228520)
at e (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1:172)
at Object.<anonymous> (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:997:393)
at e (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1:172)
at Object.ngx-dropzone-wrapper (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1981:55)
at e (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:997:33646)
at Object../node_modules/ngx-dropzone-wrapper/dist/ngx-dropzone-wrapper.ngfactory.js (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:1233:156)
at e (/home/michalis/Documents/Boxes/18-24/admin/dist/server.js:997:33646)

As you can see to my webpack.config.js file I add ngx-dropzone-wrapper to whitelist. Do you have any idea what is going on? Is there any global solution for all external libraries?

Michalis
  • 6,686
  • 13
  • 52
  • 78

1 Answers1

0

I think it's probably the fact that there is no DOM on the server-side and either your code or code of some dependency may do some direct DOM manipulation

SirDieter
  • 309
  • 2
  • 9
  • I know that is something like that... And that's the reason of the whitelist of node_modules. All other modules seems to be ignored that way... but this module... not – Michalis Jul 15 '18 at 21:22