[英]trouble requiring a package I published to npm
我在要求发布到npm的软件包时遇到麻烦
做完之后
npm install binaryconversor
我尝试了不同的实现方式。
let conversor = require('binaryconversor');
let conversor = require('./binaryconversor');
不确定正确的方法
我发布的软件包的package.json。
{
"name": "binaryconversor",
"version": "1.0.2",
"description": "Converts binary to decimal and decimal to binary",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\"&& exit 1"
},
"author": "florovarelaa",
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/florovarelaa/binaryConversor.git"
},
"bugs": {
"url": "https://github.com/florovarelaa/binaryConversor/issues"
},
"homepage": "https://github.com/florovarelaa/binaryConversor#readme"
}
binaryconversor.js导出模块
module.exports = {
b2d: binaryToDecimal,
d2b: decimalToBinary
}
另一个目录中的tests.js文件
let conversor = require('binaryconversor');
然后我运行node tests
显示错误:
internal / modules / cjs / loader.js:583 throw err; ^
Error: Cannot find module 'binaryconversor'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (C:\Users\Floro\Documents\Programacion\javascript\Node\tests\tests.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
没有index.js存在问题,这可能不是解决错误的最佳方法,但是将包含module.exports的文件更改为index.js可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.