npm ls fails if non-module files/directories exist in node_modules directory


(spike grobstein) #1

this was created over in github a while back, but it doesn’t look like any action was taken on it

My operating system has created an invisible file inside one of my scope’s directories in my global module directory and this causes npm to throw a nasty error (and return non-zero) when trying to get a listing of installed modules.

Specifically, the command I’m running is:

npm ls --global --depth 0
This doesn’t happen when these files live inside the root of the global node_modules directory; only when they’re inside a scope’s directory.

In my opinion, at the least, npm should ignore invisible files in these directories. It appears that it does this for unscoped modules since .DS_Store files in those directories don’t throw any errors. It might also be good if npm would ignore anything that didn’t have a package.json in that directory since it’s not a valid module and should probably just be ignored. But I could see there being value in reporting busted modules in there, perhaps via a --strict flag or similar.

Relatedly, errors also get thrown for broken linked modules (ie: modules that were linked, but the target of the symlinks are no longer valid). I haven’t checked if this failure happens when

How can the CLI team reproduce the problem?

The quickest way to reproduce this problem is to touch a file inside a scope’s directory and run npm ls --global --depth 0 (paths sanitized):

mkdir path/to/global/node_modules/@testscope touch path/to/global/node_modules/@testscop/.DS_Store
$ npm ls --global --depth 0
├── error: ENOTDIR: not a directory, open path/to/global/node_modules/@testscope/.DS_Store/package.json
├── grunt-cli@1.2.0
├── gulp@3.9.1
├── imagemin-cli@1.0.0
├── mime@2.3.1
├── neovim@4.1.0
├── npm@5.7.1
├── standard-version@4.3.0
├── uglify-js@3.4.3
├── vue-cli@2.9.3
└── yo@2.0.3

npm ERR! error in path/to/global/node_modules/@testscope/.DS_Store: ENOTDIR: not a directory, open ‘path/to/global/node_modules/@testscope/.DS_Store/package.json’
supporting information:

npm -v prints: 5.7.1
node -v prints: v8.9.4
npm config get registry prints: https://npm.mycompany.com/