简体   繁体   English

Electron 版本 12 错误:未捕获错误:找不到模块

[英]Electron version 12 error: Uncaught Error: Cannot find module

I've already configured nodeIntegration , contextIsolation and enableRemoteModule in main.js.我已经在 main.js 中配置nodeIntegrationcontextIsolationenableRemoteModule But the following message still appears:但仍会出现以下消息:

This error only happens when I try to import the lib.js file through the script.js.此错误仅在我尝试通过 script.js 导入 lib.js 文件时发生。

Uncaught Error: Cannot find module './lib'
Require stack:
- C:\Users\sergi\Documents\Desenvolvimento\phoras\electron-quick-start\app\index.html
    at Module._resolveFilename (internal/modules/cjs/loader.js:887)
    at Function.o._resolveFilename (electron/js2c/renderer_init.js:33)
    at Module._load (internal/modules/cjs/loader.js:732)
    at Function.f._load (electron/js2c/asar_bundle.js:5)
    at Function.o._load (electron/js2c/renderer_init.js:33)
    at Module.require (internal/modules/cjs/loader.js:959)
    at require (internal/modules/cjs/helpers.js:88)
    at script.js:1

I am using the following version:我正在使用以下版本:

  • Electron: v12.0.2 Electron:v12.0.2

  • NodeJS: v12.5.0节点JS:v12.5.0

  • NPM: v6.9.0 NPM:v6.9.0

  • I'm using electron from that repository: repository我正在使用来自该存储库的 electron:存储库

Below are the files that I am using以下是我正在使用的文件

app/ index.html应用程序/索引.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index</title>
</head>
<body>
    <script type="module" src="./js/script.js"></script>
</body>
</html>

app/js/ script.js应用程序/js/script.js

const {lib} = require('./lib'); // this is where the error is happening

lib.message('hello');

app/js/ lib.js应用程序/js/lib.js

function message(msg) {
    console.log(msg);
}

main.js main.js

const {app, BrowserWindow} = require('electron')
const path = require('path')

function createWindow () {

  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    }    
  })
  mainWindow.loadFile('app/index.html')
}

app.whenReady().then(() => {
  createWindow()
  app.on('activate', function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

What can I do to fix this error when trying to import lib.js?尝试导入 lib.js 时如何解决此错误?

Try this lib method试试这个库方法

 export function square(x) { return x * x; } 

call that way ---这么叫——

import { square} from 'lib';
 console.log(square(11));

----- OR ------ - - - 或者 - - -

import * as lib from 'lib';
 console.log(lib.square(11));

must read Module systems for JavaScript必须阅读 JavaScript 的模块系统

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM