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 :
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...
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
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
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 npmjs.org or GitHub.
Just make sure that you have a .gitignore file in your module folder that excludes node_modules.
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!