Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve module `perf_hooks`

I am trying to use perf_hooks from the nodeJS Performance API in my React Native project. Below is my code

import {performance} from 'perf_hooks';

export const measure = (
  target: Object,
  propertyKey: string,
  descriptor: PropertyDescriptor
) => {
  const originalMethod = descriptor.value;
  descriptor.value = function (...args) {
    const start = performance.now();
    const result = originalMethod.apply(this, args);
    const finish = performance.now();
    console.log(`Execution time: ${finish - start} milliseconds`);
    return result;
  };

  return descriptor;
};

I keep getting the same error. error: bundling failed: Error: Unable to resolve module perf_hooks from src/utils/metrics.ts: perf_hooks could not be found within the project.

Performance API is available from node version 8.x and my node -v response is 10.16.1.

I see that there is perf_hooks present in my node_modules as well under the @types/node/perf_hooks folder.

like image 733
varun naagaraj Avatar asked Nov 15 '22 06:11

varun naagaraj


1 Answers

First import node types

$ npm i --save-dev @types/node

Now change the import statement to this:

import { performance } from 'perf_hooks';
like image 90
cham Avatar answered Dec 27 '22 08:12

cham