Npm package dependencies not found

Hello, I'm trying to create nodes that connect to an API, I wanted to use fetch (node-fetch) which seems to me a simple solution to make requests to the API. But when I test my node, it sends me the error :

[ERR_MODULE_NOT_FOUND]: Cannot find package 'node-fetch' imported from <directory of node>

I did however declare it in my package.json :

"dependencies": {
    "node-fetch": "^2.6.9"

and I wanted to import it with this code placed at the first line of my .js document of the node :

const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args));

Where did I make a mistake?

Node-RED version : 3.0.2
Node.js version : v16.13.2
npm version : 9.6.4

Just a sanity check:

What does npm ls node-fetch show in your Node RED user Directory

Show this :

node-red-project@0.0.1 C:\Users\xxx\.node-red
`-- node-fetch@2.6.9

And your node is also installed in this location?
Have you restarted Node RED after making changes to the code/package.json?
ran npm install?

usually, it should show a hierarchy, similar to the below (I'm on a pi - so paths will differ)

root@raspberrypi:~/.node-red# npm ls node-fetch
node-red-project@0.0.1 /root/.node-red
└─┬ my-custom-node@8.0.0
  └── node-fetch@10.2.0

Yes, I have my custom node here and restart node-red after making changes and install my custom node and node-fetch.
But I don't have a similar hierarchy...

node-red-project@0.0.1 C:\Users\xxx\.node-red
+-- @hikuikuma/node-red-contrib-twitch-api@0.0.1 -> .\D:\nodeRed\node-red-contrib-twitch-api
+-- @node-red-contrib-themes/theme-collection@3.0.6
`-- node-fetch@2.6.9

It's because work on Windows?

Ok - not sure what else to look at.

I don't do development on Windows for Node JS, plenty here who do though.

I don't think it should make any difference with using symlinks to your node - but then I don't know if windows has some quirks with relative paths :man_shrugging:

i.e Windows is trying to locate it under
D:\nodeRed\node-red-contrib-twitch-api\node_modules\node-fetch even though one exists in the Node RED home folder

Did you run npm install in
D:\nodeRed\node-red-contrib-twitch-api or the .node-red directory - don't think it will make a difference to be honest :man_shrugging:

I think that the problem here is that when running in a development mode, I don't think that your node module is checking what would normally be the "parent" folder, e.g. the Node-RED userDir.

For testing, make sure that you run npm install in the node package's folder and it should work. This sorts itself out when running in a more production mode where you've installed the node via or GitHub.

Just make sure that you have a .gitignore file in your module folder that excludes node_modules.


I believe you can test this further.

in your js file at the top - print this out to console.

You will get a print out similar to.
this should show what paths the Node module will use to search for the module


This is indeed the behavior to see, once the node-fetch package is installed in my package folder, it works very well!

In my case, this loads all the "node_modules" folders from the node folder, up to the root of the disk which is my dev disk (D:) but my instance of Node RED is running on my main disk (C :), I think this explains it.

Infact when installing the package I'm developing with the path to the folder on D:\ disk, it create a symlink to this folder in the "node_modules" folder of my Node RED instance, so it couldn't find "node-fetch" which was installed in this folder...

Thank you very much for your help, I will be able to continue my development and I hope to be able to offer a new package of nodes soon to the community!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.