【问题标题】:trouble requiring a package I published to npm需要我发布到 npm 的包时遇到问题
【发布时间】: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


【解决方案1】:

没有 index.js 有问题,这可能不是修复错误的最佳方法,但是将包含 module.exports 的文件更改为 index.js 解决了该问题。

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 2019-11-24
    • 2018-02-26
    • 2012-08-08
    • 2021-06-17
    • 2021-10-16
    • 2021-08-17
    • 2022-01-06
    • 2019-12-24
    相关资源
    最近更新 更多