draeag 2021-04-02 14:42 采纳率: 0%
浏览 839

关于 import require使用冲突的问题

按如下步骤创建的项目

npx create-react-app cj-notebook
cd cj-notebook
npm start

这里能正常启动web项目

2.Web项目转Elect项目

安装electron

npm install -save electron

创建下面文件

\app\main\index.js

输入如下内容

import electron from 'electron';
...
function createWindow() {
    // Create the browser window.
    const options = {
      title: 'CJNotebook',
      width: 1200,
      height: 786,
      minWidth: 1200,
      minHeight: 600,
....

在package.json文件中增加启动命令

 "scripts": {
    "main": "./app/main/index.js",
   ....,
    "electron-start": "electron ."
  },

启动

npm run electron-start

报如下错误:

E:\github\hechengjin\ssh\MyNotebook\cj-notebook\app\main\index.js:1
import electron from 'electron';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:986:16)
    at Module._compile (internal/modules/cjs/loader.js:1034:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1108:10)
    at Module.load (internal/modules/cjs/loader.js:935:32)
    at Module._load (internal/modules/cjs/loader.js:776:14)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12684)
    at loadApplicationPackage (E:\github\hechengjin\ssh\MyNotebook\cj-notebook\node_modules\electron\dist\resources\default_app.asar\main.js:110:16)
    at Object.<anonymous> (E:\github\hechengjin\ssh\MyNotebook\cj-notebook\node_modules\electron\dist\resources\default_app.asar\main.js:222:9)
    at Module._compile (internal/modules/cjs/loader.js:1078:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1108:10)

把 "type": "module" 加入到package.json后

 "main": "./app/main/index.js",
  "type": "module",
  "dependencies": {<!-- -->

则报如下错误:

App threw an error during load
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: E:\github\hechengjin\ssh\MyNotebook\cj-notebook\app\main\index.js
require() of ES modules is not supported.
require() of E:\github\hechengjin\ssh\MyNotebook\cj-notebook\app\main\index.js from E:\github\hechengjin\ssh\MyNotebook\cj-notebook\node_modules\electron\dist\resources\default_app.asar\main.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from E:\github\hechengjin\ssh\MyNotebook\cj-notebook\package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:13)
    at Module.load (internal/modules/cjs/loader.js:935:32)
    at Module._load (internal/modules/cjs/loader.js:776:14)
    at Function.f._load (electron/js2c/asar_bundle.js:5:12684)
    at loadApplicationPackage (E:\github\hechengjin\ssh\MyNotebook\cj-notebook\node_modules\electron\dist\resources\default_app.asar\main.js:110:16)
    at Object.<anonymous> (E:\github\hechengjin\ssh\MyNotebook\cj-notebook\node_modules\electron\dist\resources\default_app.asar\main.js:222:9)
    at Module._compile (internal/modules/cjs/loader.js:1078:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1108:10)
    at Module.load (internal/modules/cjs/loader.js:935:32)
    at Module._load (internal/modules/cjs/loader.js:776:14)

简单说就是不用 "type": "module" 报:  “import 不能用”

加上 "type": "module" 报:ERR_REQUIRE_ESM  --又不让用 require

而node_modules\electron\dist\resources\default_app.asar\main.js里面又在使用require

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const electron = require("electron");
const fs = require("fs");
const path = require("path");
const url = require("url");
const { app, dialog } = electron;
const Module = require('module');
// Parse command line options.
  • 写回答

4条回答 默认 最新

  • draeag 2021-04-02 15:34
    关注

    babel/webpack ?

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘