首页 > 解决方案 > webpack changed __dirname in node_modules

问题描述

I am using electron-forge webpack and typescript template to create a simple electron app.

When using aws-iot-device-sdk-v2 I get this error:

App threw an error during load
Error: Cannot find module '/Users/jake/Code/mir-kiosk-electron/node_modules/aws-crt/dist/bin/darwin-x64/aws-crt-nodejs'
    at webpackEmptyContext (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:594:10)
    at Object.<anonymous> (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:1090:91)
    at Object../node_modules/aws-crt/dist/native/binding.js (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:1100:30)
    at __webpack_require__ (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:21:30)
    at Object../node_modules/aws-crt/dist/native/crt.js (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:1133:35)
    at __webpack_require__ (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:21:30)
    at Object../node_modules/aws-crt/dist/index.js (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:566:26)
    at __webpack_require__ (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:21:30)
    at Object../node_modules/aws-iot-device-sdk-v2/dist/greengrass/discoveryclient.js (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:2513:19)
    at __webpack_require__ (/Users/jake/Code/mir-kiosk-electron/.webpack/main/index.js:21:30)
webpack built 648a2d4f433ce522b273 in 860ms

After some digging, in node_modules/aws-crt/dist/native/binding.js

The missing module is there. /Users/jake/Code/mir-kiosk-electron/node_modules/aws-crt/dist/bin/darwin-x64/aws-crt-nodejs.node is the actual file.

#webpack.main.config.js
module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: "./src/index.ts",
  // Put your normal webpack config below here
  module: {
    rules: require("./webpack.rules"),
  },
  resolve: {
    extensions: [".js", ".ts", ".jsx", ".tsx", ".css", ".json"],
  },
  node: {
    __dirname: true,
  },
};

#webpack.rules.js
module.exports = [
  // Add support for native node modules
  {
    test: /\.node$/,
    use: "node-loader",
  },
  {
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: "@marshallofsound/webpack-asset-relocator-loader",
      options: {
        outputAssetBase: "native_modules",
      },
    },
  },
  {
    test: /\.tsx?$/,
    exclude: /(node_modules|\.webpack)/,
    use: {
      loader: "ts-loader",
      options: {
        transpileOnly: true,
      },
    },
  },
];

So how do I stop webpack from doing that?

标签: node.jswebpackelectronaws-iot

解决方案


推荐阅读