Module not found: "./node_modules/csv-writer". Parent module folder was: "/"

I am trying to use csv-writer in my plugin. I installed it in my plugin directory with these command “npm i csv-writer”.
I added only this code in my main.js file

const createCsvWriter = require("./node_modules/csv-writer");

when i am reloading my plugin i ma getting this error “Module not found: “./node_modules/csv-writer”. Parent module folder was: “/””.
How can I solve this?

1 Like

If you installed it with npm, you should be able to import it with this line:

const createCsvWriter = require('csv-writer')

I would refer to the documentation here for more details.

Refer to Pablo’s answer below

1 Like

Hey there!

It looks like csv-writer makes use of NodeJS file system APIs (at least from what I can tell by taking a quick look at it). If that’s the case, it isn’t compatible with UXP (which doesn’t provide NodeJS APIs).

Note that (if the module was compatible) you would need to use something like webpack to be able to require() stuff from node_modules.

In this case, however, it looks like that won’t be an option as the module isn’t compatible, to begin with.

Because of this, my recommendation would be to search for a “pure” CSV parser/serializer (that doesn’t interact with the file system, but just parses strings to objects and vice versa) and then use UXP’s integrated file system APIs to actually read/write the file.

2 Likes

@siam009 Forgive my careless response - Pablo’s answer is correct! My recommendation above won’t work because of the limitations of UXP right now. To add to his answer, you’ll find that getFileForOpening() and getFileForSaving() both require user interaction via a file picker dialog.

I would refer to this post: Read currently opened images - #2 by amandah and use a similar workflow (create/save csv file in the plugin folder or the data folder).

2 Likes