I’m working on publishing a JavaScript package to npm and running into an issue with directory organization. My project has a standard layout with a src folder containing the main code and a specs folder for testing.
Here’s what I have in my package.json configuration:
The weird thing is that when I install the package, all the files from my src directory end up in the package root instead of staying in their folder. The specs directory seems to work fine and maintains its structure.
What am I doing wrong with the configuration? How do I make sure npm keeps my source files in the proper src directory when someone installs my package?
This looks like a prepublish hook problem, not package.json config. I hit something similar when my build was copying files to root before packaging. Check your package.json for scripts like prepublishOnly or prepare that run build steps. These often flatten directories without you knowing. Also compare your .gitignore and .npmignore - they sometimes conflict and npm gets weird about what to include. Try making a clean test directory with just your source files and package.json, then run npm pack. See if it still happens without build tools.
The directories field in package.json doesn’t actually affect where files go during installation - it’s mostly just documentation and hints for some tools. Your real problem is probably the main field pointing to “src/index” without the .js extension. Node can usually figure this out, but it makes packaging tools act weird sometimes. Change it to “src/index.js” to be explicit. Also check if you’ve got build scripts or prepublish hooks moving files around. I’ve seen webpack and other bundlers mess with the expected structure even when the files array looks right.
check your .npmignore file - it sometimes overrides the files array and causes weird behavior. try running npm publish --dry-run to see exactly what gets packaged. the src folder flattening might be from a build tool or postinstall script, not your package.json config.
you’re duplicating entries in the files array. remove ‘src/index.js’ since ‘src/’ already covers everything in that folder. npm gets confused with overlapping file specs and flattens the structure weird.
Been there, done that. All those manual npm config tweaks? They’re a nightmare waiting to happen. You’re basically wrestling with npm’s weird quirks when you could automate the whole thing.
I just set up an automation workflow that handles build and publish from start to finish. It watches my source files, runs tests, packages everything right, then pushes to npm.
Best part? You can make it preserve whatever directory structure you want, auto-run your specs, even handle versioning. No more wondering why npm flattens your folders or which files array entries are fighting each other.
I’ve done this for dozens of packages. Never think about packaging headaches anymore - the automation just works every time.
Had the same issue last month with my own package. It’s not just the duplicate entries - npm’s directories field is the real culprit. The directories.lib property is mostly useless and doesn’t actually control where files get installed. Here’s what worked: ditch the specific file entries and stick with just the directory entries in your files array. So you’d have “src/” and “specs/” only - no individual files listed. Double-check your main field points to the right path from package root. Run npm pack locally first to test the structure before you publish.