Vue + Electron 使用 nconf
来存储,但是只要 import nconf from 'nconf';
或者 const nconf = require('nconf');
就会报错,折腾了一整天,网上的资料很少。
这是nconf
https://github.com/indexzero/...
ERROR Failed to compile with 1 errors 上午12:28:34
error in ./node_modules/yargs/lib/platform-shims/esm.mjs
Module parse failed: Unexpected token (20:34)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| let __dirname;
| try {
> __dirname = fileURLToPath(import.meta.url);
| } catch (e) {
| __dirname = process.cwd();
@ ./node_modules/yargs/index.mjs 4:0-59 7:27-42
@ ./node_modules/nconf/lib/nconf/stores/argv.js
@ ./node_modules/nconf/lib/nconf/stores sync ^\.\/.*$
@ ./node_modules/nconf/lib/nconf.js
@ ./src/background.js
@ multi ./src/background.js
ERROR Build failed with errors.
我是不是少了啥,要怎么解决??
这是yargs中的错误代码
./node_modules/yargs/lib/platform-shims/esm.mjs
let __dirname;
try {
__dirname = fileURLToPath(import.meta.url);
} catch (e) {
__dirname = process.cwd();
}
哈哈,这问题确实不好解决,原因在于 webpack 不支持
import.meta.url
,所以打包的时候会报错。通常大家都会找错方向,比如寻求 nconf 关键词。我当时也查了很久。最后我是单独用 esm 加载 yargs 来解决的,不知道对你是否有用。详情参考:Webpack 不支持
import.meta
,利用 ESM 在浏览器里使用 yargs