V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Yukee798

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

  •  
  •   Yukee798 · Apr 7, 2021 · 2797 views
    This topic created in 1847 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
        1
    learningman  
       Apr 7, 2021 via Android
    第二个加个 as Node.fs 试试?
    Yukee798
        2
    Yukee798  
    OP
       Apr 7, 2021
    @learningman Node 下面有红线:'Node' only refers to a type, but is being used as a namespace here.
    otakustay
        3
    otakustay  
       Apr 7, 2021
    以前有 remote 模块,现在没了,你的需求搞不定的,只能走 ipc 找 main 线程搞事了
    daz996
        4
    daz996  
       Apr 7, 2021
    electron 是否有启用 nodeIntegration ?
    zhuweiyou
        5
    zhuweiyou  
       Apr 7, 2021
    正确的姿势不是 ipc ?
    buhi
        6
    buhi  
       Apr 14, 2021
    浏览器进程用不了, main 进程可以用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4951 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 09:51 · PVG 17:51 · LAX 02:51 · JFK 05:51
    ♥ Do have faith in what you're doing.