Webpack can't use __dirname?

I am trying to use node-postgres to connect my application to Postgres. The code I'm using is:

import React from 'react';
import pg from 'pg';
import fs from 'fs';
var cn = {
  host: 'localhost', // server name or IP address; 
  port: 5432,
  database: 'my_db',
  user: 'myname',
  password: 'mypass'
};

and raises an error:

index.js:5 Uncaught ReferenceError: __dirname is not defined
up  @ index.js:5
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function)  @ client.js:4
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function)  @ index.js:3
console.EventEmitter._events  @ index.js:81
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function)  @ app.js:26957
__webpack_require__ @ bootstrap 539ecc7…:19
content @ bootstrap 539ecc7…:39
__webpack_require__ @ bootstrap 539ecc7…:19
(anonymous function)  @ bootstrap 539ecc7…:39
(anonymous function)  @ bootstrap 539ecc7…:39
webpackUniversalModuleDefinition  @ universalModuleDefinition:7
(anonymous function)  @ universalModuleDefinition:10

The path to index.js provided in the console is webpack: ///./~/pg/~/pgpass/lib/index.js: 5

I tried @Renzo Poddighe's solution in how to write a file using node webkit js? but I still get the same error. I think this may have something to do with the discussions at https://github.com/nwjs/nw.js/wiki/Differences-of-JavaScript-contexts#resolving-relative-paths-to-other-scripts and https://github.com/nwjs/nw.js/issues/264 . They say that

// __dirname is not defined in webkit context, this is only node.js thing
console.log(__dirname); // undefined

and

__ dirname Node.js, JavaScript-, (). __dirname WebKit, JavaScript-, HTML, jQuery $.getScript() .

? , .

,

var config = {
  entry: { app: './src/index.jsx'},
  output: {
     libraryTarget: 'umd',
     path: path.join(__dirname, 'dist'),
     filename: '[name].js'
  }, ...

webpack.config.js :

...
  node: {
    console: true,
    __dirname: true,
    dns: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  }
...
+5
1

webpack:

{
  node: {
    __dirname: true
  }
}

webpack __dirname . context

+7

All Articles