Webpack can not use __dirname?

11,376

In that case, add this to your webpack config:

{
  node: {
    __dirname: true
  }
}

This will tell webpack to replace __dirname instances with the path of the module. This path relative to context

Share:
11,376
neallred
Author by

neallred

I love learning and helping others and I love that web development enables me to do both! My current favorite technologies are Haskell and Rust, and my forever favorites are Linux and Vim. In my spare time, I develop a purely functional library book management system for a private collection of nearly 1000 books.

Updated on September 15, 2022

Comments

  • neallred
    neallred over 1 year

    I am trying to use node-postgres to hook my app up to Postgres. The code I use 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 it produces the 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 on index.js provided in the console is webpack:///./~/pg/~/pgpass/lib/index.js:5

    I tried @Renzo Poddighe solution at how to write file with node webkit js? but I still get the same error. I think it 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 works in Node.js modules, i.e. in JavaScript code that was called with require(). __dirname doesn't work only in WebKit scripts, i.e. in JavaScript code that was called with HTML , or jQuery's $.getScript(), or any other similar method.

    Any ideas? Let me know what other information I need to include.

    edit

    I think my target is

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

    My webpack.config.js looks like:

    ...
      node: {
        console: true,
        __dirname: true,
        dns: 'empty',
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
      }
    ...
    
  • neallred
    neallred about 8 years
    I added it but I still get the same error after restarting my webpack-dev-server. I am also using hot-reloader if that makes a difference.
  • thangngoc89
    thangngoc89 about 8 years
    Is there any reason why do you trying to use fs and pg in a web environment ? Webpack has mock for node api but pg in the other hand, is a node package. It might uses some node api underhood.
  • neallred
    neallred about 8 years
    I might be going about this the wrong way, but I want to create a web interface for the database so a non-technical admin can add, edit, and delete entries in the database. I thought I needed to include pg to do this. I included fs to try and fix errors, but maybe that introduced more. If I don't include the dns, fs, tls, net, I get an error about the modules not being found.
  • thangngoc89
    thangngoc89 about 8 years
    You're doing this the wrong way. Make an REST API on top of pg, and consume it from your web app.
  • Seth Eden
    Seth Eden almost 4 years
    I ran into this same problem when trying to setup a custom Webpack for my Electron App. Now I am getting an error: ERROR Invalid options in vue.config.js: "node" is not allowed
  • Michael Paccione
    Michael Paccione about 3 years
    I tried the above and it doesn't work for me. :/