'npm ls'显示模块,但是require()找不到它

时间:2019-02-21 22:21:18

标签: javascript node.js npm

我已安装模块type

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

但是require找不到它:

~\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' }

我该如何解决?

2 个答案:

答案 0 :(得分:0)

您需要程序包在module.paths中查找。可能没有正确的路径C:\Users\user\Documents\appname

您是:

  • 设置相对路径require('.type')
  • 设置标准路径require('C:\Users\user\Documents\appname\type')

答案 1 :(得分:0)

在这种情况下,问题是由a badly formed package通过以下方式创建的:

"main": "checks.js",行添加到package.json可使模块正常工作

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] }
>