node.js - 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?
解决方案
推荐阅读
- algorithm - 获取网格中的连续面
- git - 尝试拉时Docker symfony 4权限被拒绝
- r - Layering polygons and location points in r
- angular - Error: Component X is not part of any NgModule or the module has not been imported into your module
- ruby-on-rails - 我需要一种方法来删除非德语字母并且只允许特定字符
- tableau-api - 有没有办法在 Tableau 中访问存储的报表和图表查询?
- php - 为什么 Cron 调度程序在 laravel 中不起作用?
- sql - 分组记录时如何使用 ROW_NUMBER?
- java - 在不同项目中用作已编译 jar 时,Spring AOP 不起作用
- javascript - 当 Datatables 已经初始化时,如何从 AJAX 响应中播种数据?