Standard folder to put files for bootsrapping a package?


#1

I need to create/download some files as part of the package I’m building. Not really temp files, but part of bootstrapping the functionality. I guess they should best be put inside the package’s folder. Can I get a path for the package itself so I can write out stuff there? Or is there some other established way?

Cheers,
Viktor


#2

Inside a node module you have the __dirname variable that points to the directory containing the module


#3

@olmokramer Thanks! That doesn’t seem to be entirely true though, it is the folder of the current script, so in my case that is $module/lib. https://nodejs.org/docs/latest/api/globals.html#globals_dirname. Kindof works but would rather put it relative the package root so stuff doesn’t break if I move code around.

I remember seeing some atom code where someone did something with filesystem of a dependency package, but I can’t seem to find it…


#4

Oh, I guess I could just use:

But it would be even nicer to know which package I’m in without hardcoding the name of my package…


#5

You can walk up the directory path until you find the package.json and read the package name out of there.


#6

In node speak every separate file is a module, but yes I meant the current script.


#7

If your package’s main file is in the same folder as your package, as opposed to the lib subdirectory, then you could populate a variable in your package’s main file, and I guess you could then reference that variable from everywhere?