Install tensorflow with node.js can not use

Hello i’m a newbie for tensorflow, Now i use Node.js is core language and already install tensorflow by npm

  • @tensorflow/tfjs
  • @tensorflow/tfjs-node
  • @tensorflow/tfjs-node-gpu

and require like this

  • const tf = require(’@tensorflow/tfjs’)
  • require(’@tensorflow/tfjs-node’)
  • require(’@tensorflow/tfjs-node-gpu’)

But when I try to use it occur error
const tf = require(’@tensorflow/tfjs’)
TypeError: require is not a function

How to solve it

Thank you

TensorFlow.js is a library for machine learning in JavaScript. Develop ML models in JavaScript, and use ML directly in the browser or in Node.js.

Hi, @puangpaka20

Welcome to TensorFlow Forum!

I apologize for the delayed response and I see you’re importing multiple libraries like
@tensorflow/tfjs , @tensorflow/tfjs-node , @tensorflow/tfjs-node-gpu
at the same time after installing them into your Node.js project so either you can import CPU TensorFlow.js for Node like const tf = require('@tensorflow/tfjs-node'); or GPU TensorFlow.js for Node like const tf = require('@tensorflow/tfjs-node-gpu');

Note: you do not need to add the @tensorflow/tfjs package to your dependencies or import it directly.

Make sure that you’ve added "type": "commonjs" in package.json file

  • For ES modules: Use import statements for browser-friendly module loading:
import * as tf from '@tensorflow/tfjs-node' # CPU TensorFlow.js for Node
OR import * as tf from '@tensorflow/tfjs-node-gpu' # GPU TensorFlow.js for Node

Make sure that you’ve added "type": "module" in package.json file for ES modules import.

I hope it will help you to solve your issue. just let me know if I have missed something here. Thank you for your understanding and patience.