V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
pcell
V2EX  ›  Vue.js

为什么用 vue add electron-builder 后创建的应用编译后无法使用某些第三方模块?

  •  
  •   pcell · 2021-01-21 01:11:49 +08:00 · 2195 次点击
    这是一个创建于 1184 天前的主题,其中的信息可能已经有所发展或是发生改变。
    package.json 中 dependencies 有 chokidar 和 iconv-lite,npm run electron:serve 运行时能通过 window.require("chokidar") 的形式正常加载,但使用 npm run electron:build 编译后的 exe 无法加载这两个模块,这是为什么? mitt 和 nodejs 的 fs 模块都能正常加载使用。
    错误信息如下:
    Uncaught Error: Cannot find module 'chokidar'
    Require stack:
    - electron/js2c/renderer_init
    at Module._resolveFilename (internal/modules/cjs/loader.js:797)
    at Function.i._resolveFilename (electron/js2c/renderer_init.js:43)
    at Module._load (internal/modules/cjs/loader.js:690)
    at Function.Module._load (electron/js2c/asar.js:769)
    at Module.require (internal/modules/cjs/loader.js:852)
    at require (internal/modules/cjs/helpers.js:74)
    at Module.56d7 (app.dc68e4fd.js:1)
    at l (app.dc68e4fd.js:1)
    at Object.0 (app.dc68e4fd.js:1)
    at l (app.dc68e4fd.js:1)


    我的 package.json 内容( mammoth 没有用上):
    {
    "name": "electron-xx",
    "version": "0.1.0",
    "private": true,
    "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service electron:build",
    "electron:serve": "vue-cli-service electron:serve",
    "postinstall": "electron-builder install-app-deps",
    "postuninstall": "electron-builder install-app-deps"
    },
    "main": "background.js",
    "dependencies": {
    "chokidar": "^3.5.1",
    "core-js": "^3.6.5",
    "iconv-lite": "^0.6.2",
    "mammoth": "^1.4.16",
    "mitt": "^2.1.0",
    "vue": "^3.0.0"
    },
    "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "babel-eslint": "^10.1.0",
    "electron": "9.4.1",
    "electron-devtools-installer": "^3.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0-0",
    "node-sass": "^5.0.0",
    "sass-loader": "^10.1.1",
    "vue-cli-plugin-electron-builder": "^2.0.0-rc.5"
    },
    "eslintConfig": {
    "root": true,
    "env": {
    "node": true
    },
    "extends": [
    "plugin:vue/vue3-essential",
    "eslint:recommended"
    ],
    "parserOptions": {
    "parser": "babel-eslint"
    },
    "rules": {}
    },
    "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
    ]
    }
    4 条回复    2021-10-20 15:23:23 +08:00
    zzk1989
        1
    zzk1989  
       2021-01-21 11:23:38 +08:00
    pcell
        2
    pcell  
    OP
       2021-01-21 12:18:02 +08:00 via Android
    @zzk1989 好的好的,我回去试试
    pcell
        3
    pcell  
    OP
       2021-01-21 18:56:30 +08:00
    @zzk1989 可以了,谢谢!补充完整一点细节:新建一个和 package.json 同目录的 vue.config.js 加上
    module.exports = {
    pluginOptions: {
    electronBuilder: {
    externals: ['chokidar', 'iconv-lite']
    }
    }
    }
    Mark7Dai
        4
    Mark7Dai  
       2021-10-20 15:23:23 +08:00
    @pcell 太感谢了!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2852 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:19 · PVG 22:19 · LAX 07:19 · JFK 10:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.