5

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
  • 8,203
  • 10
  • 44
  • 66
  • 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

4

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

// webpack.config.js

module.exports = {
  //...
  externals: {
    'cross-fetch': 'fetch' 
  }
};
felixmosh
  • 32,615
  • 9
  • 69
  • 88
  • wouldn't that should be `'cross-fetch': 'fetch'` instead. The left part match the import and the right part how to handle it and replace the import ??? – Mohamed Allal Aug 30 '22 at 11:08