【发布时间】:2020-01-10 18:46:42
【问题描述】:
我在需要我发布到 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
显示错误:
内部/模块/cjs/loader.js:583 抛出错误; ^
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)
【问题讨论】:
-
你用
npm install安装包了吗?
标签: javascript node.js npm require