nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.13k stars 29.36k forks source link

require.resolve "Error: Cannot find module" even though package folder exists #29549

Closed TotallyInformation closed 5 years ago

TotallyInformation commented 5 years ago

require.resolve will not find the module startbootstrap-sb-admin-2 even though it was installed successfully and confirmed as existing in ./node_modules.

This happens on Linux as well as Windows and on node v8 and v10.

I have tried deleting the package folder and forcing a cache clean.

Code to demonstrate:

cd C:\src\nr\data
npm remove startbootstrap-sb-admin-2
npm cache clean --force
npm install startbootstrap-sb-admin-2
npm list startbootstrap-sb-admin-2
node ./test.js

test.js

// sprintf-js is installed under the directory of this script
var t1 = require.resolve('sprintf-js')
console.log(t1,'\n\n')

// startbootstrap-sb-admin-2 is installed under the directory of this script
try {
    var t2 = require.resolve('startbootstrap-sb-admin-2')
    console.log(t2)
} catch (e) {
    console.error(e)
    var t3 = require.resolve.paths('startbootstrap-sb-admin-2')
    console.log(t3)
}

Actual output from test.js

C:\src\nr\data
λ  node test.js
C:\src\nr\data\node_modules\sprintf-js\src\sprintf.js 

{ Error: Cannot find module 'startbootstrap-sb-admin-2'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.resolve (internal/modules/cjs/helpers.js:30:19)
    at Object.<anonymous> (C:\src\nr\data\test.js:8:22)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19) code: 'MODULE_NOT_FOUND' }

[ 'C:\\src\\nr\\data\\node_modules',
  'C:\\src\\nr\\node_modules',
  'C:\\src\\node_modules',
  'C:\\node_modules',
  'C:\\Users\\user\\.node_modules',
  'C:\\Users\\user\\.node_libraries',
  'C:\\Program Files\\nodejs\\lib\\node' ]

C:\src\nr\data

Actual output from npm list startbootstrap-sb-admin-2

C:\src\nr\data
λ  npm list startbootstrap-sb-admin-2
node-red-project@0.0.4 C:\src\nr\data
`-- startbootstrap-sb-admin-2@4.0.6
shobhitchittora commented 5 years ago

@TotallyInformation The module startbootstrap-sb-admin-2 doesn't have an entrypoint defined ( main ) in it's package.json. I think that's the reason it's not being resolved by the package resolution logic.

TotallyInformation commented 5 years ago

Doh! OK, thanks for that - it would be really helpful to have that information in the docs for require.resolve.

I have a need to be able to find the installation folder for any installed package and I've been relying on require.resolve but from this it is clear that I cannot rely on it and will have to find an alternative approach.

TotallyInformation commented 5 years ago

In case anyone else has the same issue, I added a work around that manually looks for a folder name matching the package name in the node_modules folder of the projects folder.

node-red-contrib-uibuilder v2.0.3 implements it.