-1

I wrote up a small library, just a .js file with some frequently used functions, and dropped it into the node_modules folder so it was alongside all of my other packages. Great.

A few days later, I added a new package with npm install. When I ran the program to test it, I got errors for my library missing, and sure enough it was no longer in the node_modules folder, recycling bin, or even appearing in my source control because I gitignore'd the folder.

As someone who's new to projects that use more than a handful of files, could someone please explain what may have caused this, and how I can prevent it in the future?

Olivinism
  • 9
  • 2
  • 2
    node_modules is only for files downloaded by npm. JS files that you write belong in your `source` or `src` folder, which lives outside of node_modules. – Peter B Jun 25 '22 at 00:58
  • You may well want to do something like this: https://stackoverflow.com/q/15806241 Have a separate folder for your other module somewhere else on the filesystem, and link the directory. – CertainPerformance Jun 25 '22 at 01:03
  • On the version control side, I also recommend making small enough commits that you can see the list of files that changed and hopefully notice that a file you care about isn’t being committed. – Ry- Jun 25 '22 at 02:54

1 Answers1

2

node_modules isn’t for your own files; it’s managed entirely by npm based on the contents of your package.json. You should put those in the directory that contains node_modules, and load them with relative imports (import foo from './foo' or require('./foo')).

Ry-
  • 218,210
  • 55
  • 464
  • 476