0

I have the module type installed:

~\Documents\appname $ npm ls type
serverless-commerce@5.0.0 C:\Users\user\Documents\appname
`-- type@0.0.3

However require cannot find it:

~\Documents\appname $ node
> require('type')
{ Error: Cannot find module 'type'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15) 
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18) code: 'MODULE_NOT_FOUND' }

How can I fix this?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494

2 Answers2

0

You are requiring the package to look in module.paths. which may not have the right path C:\Users\user\Documents\appname.

You an either:

  • set a relative path require('.type')
  • set a fully qualified path require('C:\Users\user\Documents\appname\type')
Erik K
  • 471
  • 5
  • 12
  • My understanding in that require looks in `node_modules` recursively - and that if `npm ls` shows it, it should be findable by `require`. Otherwise I'd have to use a relative or FQ path names for every module - which I (and every does node user) does not. – mikemaccana Feb 21 '19 at 22:28
  • it also may just be a windows thing? https://stackoverflow.com/questions/9587665/nodejs-cannot-find-installed-module-on-windows – Erik K Feb 22 '19 at 11:32
0

In this case, the issue was created by a badly formed package with:

Adding the line "main": "checks.js", to package.json makes the module work

node
> require('type')
{ isBoolean: [Function: isBoolean],     
isUndefined: [Function: isUndefined], 
isNull: [Function: isNull],
isString: [Function: isString],
isNumber: [Function: isNumber],
isObject: [Function: isObject],
instanceOf: [Function: instanceOf],
isRegExp: [Function: isRegExp],
isDate: [Function: isDate],
isFunction: [Function: isFunction],
isArray: [Function: isArray],
isArguments: [Function: isArguments],
isPrimitive: [Function: isPrimitive],
isFlat: [Function: isFlat],
isEmpty: [Function: isEmpty],
isJSON: [Function] }
>
mikemaccana
  • 110,530
  • 99
  • 389
  • 494