Skip to content

node target should ignore hashbang #2168

Description

@ajcrites

When building for the node target, the hashbang should be ignored. For example, if you have a file that has require("JSONStream") you will get an error because this library's main file includes a hashbang

The node interpreter itself will ignore the hashbang if you run the file, but webpack will treat this as an error.

Activity

  1. AndrewGrachov commented on Aug 30, 2016

    @AndrewGrachov

    upvote

  2. martriay commented on Sep 7, 2016

    @martriay

    upvote

  3. Alexandre-Herve commented on Sep 22, 2016

    @Alexandre-Herve

    Did you find a workaround ? I have this problem with vendor JSONStream lib, don't know what to do !

  4. martriay commented on Sep 22, 2016

    @martriay
  5. komplexb commented on Oct 25, 2016

    @komplexb

    Hey @martriay, I'm having the same issue, but I'm unclear on how to use shebang-loader. Is it used in the webpack.config.js file? Could you post a snippet of it in use? Thanks.

  6. martriay commented on Oct 25, 2016

    @martriay

    @komplexb sure! As I had troubles with the JSONStream lib, my loader looks like this:

    {
      test: /node_modules\/JSONStream\/index\.js$/,
      loaders: ['shebang', 'babel']
    }
    
  7. komplexb commented on Oct 25, 2016

    @komplexb

    @martriay thanks for that.
    So it's ignoring it, as in the shebang no longer stops my build. But I assumed it would leave the #!/usr/bin/env node --harmony at the top of my built file; or is that not how it works?

    module: {
        loaders: [
          {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: ['shebang', 'babel'],
          }
        ]
      },
    
  8. bjackson commented on Mar 25, 2017

    @bjackson

    Any update on this? It seems like it might be a good feature to be built in, so that external packages don't need to be updated (as the hashbang is allowed by node).

  9. Nevraeka commented on May 16, 2017

    @Nevraeka

    Any update this?

  10. Kielan commented on Jun 29, 2017

    @Kielan

    how is there not a solution to this. the only recommended shebang package doesn't even explain how to use it on it's own github page.

  11. kyledecot commented on Jul 5, 2017

    @kyledecot

    Also running into this. Any update on an "official" fix?

  12. Zenbis commented on Jul 6, 2017

    @Zenbis

    Where do i drop the code { test: /node_modules\/JSONStream\/index\.js$/, loaders: ['shebang', 'babel'] }

  13. kthakre9 commented on Jul 19, 2017

    @kthakre9

    any updates on this fix?

  14. mach-kernel commented on Aug 4, 2017

    @mach-kernel

    Also running into this issue. This is affecting our web scale. Please fix.

  15. rsimonton commented on Sep 22, 2017

    @rsimonton

    upvote

  16. CaerusKaru commented on Sep 23, 2017

    @CaerusKaru

    To use in latest versions of webpack:

    module: {
        rules: [
          ...
          { test: /FILE_NAME/, loader: 'shebang-loader', include: [/node_modules\/MODULE_NAME/] },
          ...
        ]
      }
    
  17. imerkle commented on Oct 14, 2017

    @imerkle

    shebang-loader not working used the above ^^ code with webpack 3

  18. fxlemire commented on Oct 15, 2017

    @fxlemire

    If you want to use shebang-loader and you are on Windows the following will work on webpack 3:

    module: {
      rules: [
        {
          test: path.resolve(__dirname, 'node_modules/jsonstream/index.js'),
          use: 'shebang-loader',
        },
      ],
    },
    

    The above solutions are not working on Windows because of the path separators (/ vs \)

  19. mnemanja commented on Oct 21, 2017

    @mnemanja

    I'm having the same problem with the sqlite3 library when using it with electron and create-react-app, where I have no access to the Webpack (would like not to have to eject).

  20. fxlemire commented on Oct 21, 2017

    @fxlemire

    @mnemanja What you can do as a last resort is import it with the ! syntax

    import * as sqlite3 from 'shebang-loader!sqlite3';

    Problem is if you intend to write tests... This approach will break your jest tests since it does not use webpack. To fix it, you'd have to eject to be able to configure jest. Ironic right?

    In my specific case of JSONStream I ended up using a fork, jsonstream2, which got rid of the shebang line, among other things. You could maybe do that too with sqlite3.

    Good luck!

  21. mnemanja commented on Oct 21, 2017

    @mnemanja

    @fxlemire Thanks for the suggestion!

    I'll change the approach that I'm taking so that I'm going over the main process (in the structure my main process file is in the public folder so I was looking for an alternative and handle everything from inside of the render process).

    Have a nice weekend :)

  22. mrchief commented on Oct 25, 2017

    @mrchief

    For cross OS regex match, use this expression:

    include: /node_modules[/\\]MODULE_NAME/

    this will work in windows and linux environments alike.

  23. mrchief commented on Oct 25, 2017

    @mrchief

    This one is tested with webpack3 on Windows and works:

    module: {
      rules: [
        {
            test: /node_modules[/\\]jsonstream/i
            , loader: 'shebang-loader'
        }
      ]
    }
  24. johan44co commented on Nov 8, 2017

    @johan44co

    in your module.exports at webpack.config.js include
    externals: { "JSONStream": "require('JSONStream')" },
    and if you are in a Electron or Node environment don't forget to set target
    target: 'electron-renderer',

  25. ooflorent commented on Jan 9, 2018

    @ooflorent
    Contributor

    Superseded by #4603

  26. govindrai commented on May 11, 2019

    @govindrai

    Any update to reflect this within webpack itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions