1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

node js server side: How can I import d3 and make it available?

Discussion in 'JavaScript' started by SoftLink, Jul 2, 2025 at 4:40 AM.

  1. #1
    I am trying to produce a png image of a word cloud using node js and d3-cloud.

    I've got a Debian 11 server running node js.
    I have installed d3: /usr/lib/node_modules/d3 using npm -g for global use.
    I have also installed d3-cloud: /usr/lib/node_modules/d3-cloud using npm -g for global use..

    I have looked in the /usr/lib/node_modules folder and both d3-cloud and d3 folders are inside it. I looked in the d3 folder and it does have a package.json and a node_modules folder with all of d3's sub modules (ie d3-array).

    The d3-cloud folder also contains a node_modules folder:
    /usr/lib/node_modules/d3-cloud/node_modules containing it's sub module d3-dispatch.

    chatgpt is clueless.

    I run node js from php: echo shell_exec("node '/.../TestCloud.js'");
    The user is www-data.

    Skia-canvas has been tested with chart.js and it works fine.
    I cannot get d3-cloud to produce a word cloud.
    This works fine so I can require skia-canvas and d3-cloud with no problem:
    
    
    require('module').globalPaths.push('/usr/lib/node_modules');
    const { Canvas } = require('skia-canvas');
    //const d3 = require('d3');
    const cloud = require('d3-cloud');
    console.log("GOT d3-cloud");
    
    Code (markup):
    It does echo "GOT d3-cloud".

    But when I run this:
    
    
    require('module').globalPaths.push('/usr/lib/node_modules');
    const { Canvas } = require('skia-canvas');
    const d3 = require('d3');
    const cloud = require('d3-cloud');
    console.log("GOT d3");
    
    Code (markup):
    It does NOT echo "GOT d3".

    How can I successfully require d3 in a node js file?
    More importantly, how can I make d3-cloud produce a png image of a word cloud?
     
    SoftLink, Jul 2, 2025 at 4:40 AM IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,276
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #2
    Grok seems to be clueless about this too. It suggested:

    Check for Errors:

    try{
    const d3 =require('d3'); console.log("GOT d3");
    }catch(error){
    console.error("Error loading d3:", error.message);
    }

    Ensure Compatibility:

    Verify that the version of d3 is compatible with your Node.js version and d3-cloud. Run: npm list d3 d3-cloud

    Recommended Solution:

    The simplest and most reliable approach is to install d3 locally and avoid modifying global paths: npm install d3
    Then use this code:

    const{ Canvas }=require('skia-canvas');
    const d3 =require('d3');
    const cloud =require('d3-cloud');
    console.log("GOT d3");
     
    qwikad.com, Jul 6, 2025 at 3:20 AM IP