reactnativecn / react-native-website

React Native 中文网
https://reactnative.cn
MIT License
218 stars 328 forks source link

rn 如何使用node模块的代码 #602

Closed leo-lucky-hub closed 2 years ago

leo-lucky-hub commented 2 years ago

npm install --save universal-speedtest

import { UniversalSpeedtest, SpeedUnits } from "universal-speedtest";

const universalSpeedtest = new UniversalSpeedtest({ measureUpload: true, downloadUnit: SpeedUnits.MBps }); universalSpeedtest.runSpeedtestNet().then((result) => { console.log(result); });

错误如下: error: Error: Unable to resolve module perf_hooks from /Users/develop/ReactNative/Test/node_modules/universal-speedtest/dist/Speedtest.js: perf_hooks could not be found within the project or in these directories: node_modules 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | exports.Speedtest = void 0; > 4 | const perf_hooks_1 = require("perf_hooks"); | ^ 5 | const path = require("path"); 6 | const worker_threads_1 = require("worker_threads"); 7 | const UrllibHelper_1 = require("./helpers/UrllibHelper"); at ModuleResolver.resolveDependency (/Users/develop/ReactNative/Test/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:158:15) at DependencyGraph.resolveDependency (/Users/develop/ReactNative/Test/node_modules/metro/src/node-haste/DependencyGraph.js:231:43) at Object.resolve (/Users/develop/ReactNative/Test/node_modules/metro/src/lib/transformHelpers.js:129:24) at resolve (/Users/develop/ReactNative/Test/node_modules/metro/src/DeltaBundler/traverseDependencies.js:396:33) at /Users/develop/ReactNative/Test/node_modules/metro/src/DeltaBundler/traverseDependencies.js:412:26 at Array.reduce ()enter code here at resolveDependencies (/Users/develop/ReactNative/Test/node_modules/metro/src/DeltaBundler/traverseDependencies.js:411:33) at processModule (/Users/develop/ReactNative/Test/node_modules/metro/src/DeltaBundler/traverseDependencies.js:140:31) at async addDependency (/Users/develop/ReactNative/Test/node_modules/metro/src/DeltaBundler/traverseDependencies.js:230:18) at async Promise.all (index 4)

个人理解,是因为无法使用@type/node/perf_hooks.d.st里面的代码

sunnylqm commented 2 years ago

rn的运行环境不是node,并不能直接使用依赖node模块的代码

leo-lucky-hub commented 2 years ago

那应该如何使用node的模块呢

sunnylqm commented 2 years ago

有替代品的话用替代品,没有的话没辙

leo-lucky-hub commented 2 years ago

rn的运行环境不是node,那它的运行环境是什么?

sunnylqm commented 2 years ago

https://reactnative.cn/docs/javascript-environment

github-actions[bot] commented 2 years ago

👋 Hey there, it looks like there has been no activity on this issue in the last 90 days. Has the issue been fixed, or does it still require the community attention? This issue will be closed in the next 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 2 years ago

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.