Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node target should ignore hashbang #2168

Closed
ajcrites opened this issue Mar 11, 2016 · 26 comments

Comments

Projects
None yet
@ajcrites
Copy link

commented Mar 11, 2016

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.

@bebraw bebraw added the enhancement label Mar 11, 2016

@AndrewGrachov

This comment has been minimized.

Copy link

commented Aug 30, 2016

upvote

1 similar comment
@martriay

This comment has been minimized.

Copy link

commented Sep 7, 2016

upvote

@Alexandre-Herve

This comment has been minimized.

Copy link

commented Sep 22, 2016

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

@martriay

This comment has been minimized.

@komplexb

This comment has been minimized.

Copy link

commented Oct 25, 2016

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.

@martriay

This comment has been minimized.

Copy link

commented Oct 25, 2016

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

{
  test: /node_modules\/JSONStream\/index\.js$/,
  loaders: ['shebang', 'babel']
}
@komplexb

This comment has been minimized.

Copy link

commented Oct 25, 2016

@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'],
      }
    ]
  },
@bjackson

This comment has been minimized.

Copy link

commented Mar 25, 2017

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).

@Nevraeka

This comment has been minimized.

Copy link

commented May 16, 2017

Any update this?

@Kielan

This comment has been minimized.

Copy link

commented Jun 29, 2017

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.

@kyledecot

This comment has been minimized.

Copy link

commented Jul 5, 2017

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

@Zenbis

This comment has been minimized.

Copy link

commented Jul 6, 2017

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

@kthakre9

This comment has been minimized.

Copy link

commented Jul 19, 2017

any updates on this fix?

@mach-kernel

This comment has been minimized.

Copy link

commented Aug 4, 2017

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

@rsimonton

This comment has been minimized.

Copy link

commented Sep 22, 2017

upvote

@CaerusKaru

This comment has been minimized.

Copy link

commented Sep 23, 2017

To use in latest versions of webpack:

module: {
    rules: [
      ...
      { test: /FILE_NAME/, loader: 'shebang-loader', include: [/node_modules\/MODULE_NAME/] },
      ...
    ]
  }
@imerkle

This comment has been minimized.

Copy link

commented Oct 14, 2017

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

@fxlemire

This comment has been minimized.

Copy link

commented Oct 15, 2017

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 \)

@mnemanja

This comment has been minimized.

Copy link

commented Oct 21, 2017

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).

@fxlemire

This comment has been minimized.

Copy link

commented Oct 21, 2017

@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!

@mnemanja

This comment has been minimized.

Copy link

commented Oct 21, 2017

@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 :)

@mrchief

This comment has been minimized.

Copy link

commented Oct 25, 2017

For cross OS regex match, use this expression:

include: /node_modules[/\\]MODULE_NAME/

this will work in windows and linux environments alike.

@mrchief

This comment has been minimized.

Copy link

commented Oct 25, 2017

This one is tested with webpack3 on Windows and works:

module: {
  rules: [
    {
        test: /node_modules[/\\]jsonstream/i
        , loader: 'shebang-loader'
    }
  ]
}
@johan44co

This comment has been minimized.

Copy link

commented Nov 8, 2017

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',

@ooflorent

This comment has been minimized.

Copy link
Member

commented Jan 9, 2018

Superseded by #4603

@govindrai

This comment has been minimized.

Copy link

commented May 11, 2019

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
You can’t perform that action at this time.