2

I am importing a third-party module which imports another module:

import fetch from "cross-fetch";

I would like to tell Webpack to ignore/remove this import because the variable fetch already exists in global namespace. Is that possible?

Mick
  • 6,858
  • 6
  • 36
  • 60
  • So, are you trying to tell Webpack not to throw an error? –  Jan 01 '20 at 16:06
  • I just want to skip this import. `fetch` already exists in global namespace provided by the serverless enviroment I use. I want the third-party module NOT to import cross-fetch and instead use the already existent global variable. – Mick Jan 01 '20 at 16:07
  • Does this answer your question? [How can I make webpack skip a require](https://stackoverflow.com/questions/34828722/how-can-i-make-webpack-skip-a-require) –  Jan 01 '20 at 16:10
  • I tried to use IgnorePlugin but then the import statement is bundled and throws an exception. The import statement should be removed instead. – Mick Jan 01 '20 at 16:12
  • Did you try the highest voted answer's solution?: ```const myCustomModule = eval('require')(myCustomPath)``` –  Jan 01 '20 at 16:13
  • This is not related to my problem. Also I am not doing the import, the third-party module does and even if, i cant use `eval` in my serverless environment. – Mick Jan 01 '20 at 16:15

1 Answers1

3

You can specify that some module is in the global env with externals.

// webpack.config.js

module.exports = {
  //...
  externals: {
    fetch: 'cross-fetch'
  }
};
felixmosh
  • 26,970
  • 6
  • 57
  • 80