Yukee798

有什么办法能够在 React 中使用 import 导入 nodejs 模块吗?

  •  
  •   Yukee798 · Apr 7, 2021 · 3189 views
    This topic created in 2042 days ago, the information mentioned may be changed or developed.

    最近在用 React + Electron + ts 写 App,有需求要用到 nodejs 里面的 fs 模块,了解到了几种导入方式。

    // 这样导入会报错:Module not found: Can't resolve 'fs',但是有代码提示
    import * as fs from 'fs';
    
    // 使用 require 导入
    // 可以正常使用但是没有代码提示
    const fs = window.require('fs');
    
    6 replies  •  2021-04-14 10:25:36 +08:00
    learningman
    learningman
    Apr 7, 2021 via Android
    1
    第二个加个 as Node.fs 试试?
    Yukee798
    Yukee798
    OP
    Apr 7, 2021
    2
    @learningman Node 下面有红线:'Node' only refers to a type, but is being used as a namespace here.
    otakustay
    otakustay
    Apr 7, 2021
    3
    以前有 remote 模块,现在没了,你的需求搞不定的,只能走 ipc 找 main 线程搞事了
    daz996
    daz996
    Apr 7, 2021
    4
    electron 是否有启用 nodeIntegration ?
    zhuweiyou
    zhuweiyou
    Apr 7, 2021
    5
    正确的姿势不是 ipc ?
    buhi
    buhi
    Apr 14, 2021
    6
    浏览器进程用不了, main 进程可以用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4026 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 373ms · UTC 07:12 · PVG 15:12 · LAX 23:12 · JFK 02:12
    ♥ Do have faith in what you're doing.