无法在Windows中找到本地安装的模块

时间:2015-11-02 15:04:23

标签: node.js npm

我有一个非常简单的脚本,我用它来测试这个场景,但我无法弄清楚为什么Node无法加载本地安装的模块。

的package.json:

{
  "name": "Test",
  "version": "1.0.0",
  "dependencies": {
    "cli-progress-bar": "^0.1.0"
  }
}

Test.js:

var ProgressBar = require("cli-progress-bar"),
    progress = new ProgressBar(),
    processed = 0;

function update () {
    if (processed < 100) {
        progress.show(++processed / 100);
        setTimeout(update, 750);
    }
}

update();

据我所知,这应该只加载cli-progress-bar包,并在退出前运行一个简单的进度条完成。

这是我尝试安装依赖项并运行脚本时得到的输出:

C:\Users\Benjamin\Desktop\Test>node -v
v4.2.1

C:\Users\Benjamin\Desktop\Test>npm -v
3.3.10

C:\Users\Benjamin\Desktop\Test>npm install
Test@1.0.0 C:\Users\Benjamin\Desktop\Test
└─┬ cli-progress-bar@0.1.0
  ├── cli-character-set@0.1.0
  ├─┬ lodash.padleft@3.1.1
  │ ├── lodash._basetostring@3.0.1
  │ └─┬ lodash._createpadding@3.6.1
  │   └── lodash.repeat@3.0.1
  ├── lodash.padright@3.1.1
  └─┬ log-update@1.0.2
    ├── ansi-escapes@1.1.0
    └─┬ cli-cursor@1.0.2
      └─┬ restore-cursor@1.0.1
        ├── exit-hook@1.1.1
        └── onetime@1.0.0

npm WARN EPACKAGEJSON Test@1.0.0 No description
npm WARN EPACKAGEJSON Test@1.0.0 No repository field.
npm WARN EPACKAGEJSON Test@1.0.0 No license field.

C:\Users\Benjamin\Desktop\Test>node Test.js
module.js:339
    throw err;
    ^

Error: Cannot find module 'cli-progress-bar'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous> (C:\Users\Benjamin\Desktop\Test\Test.js:1:81)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Function.Module.runMain (module.js:467:10)

我可以看到该软件包已安装在C:\Users\Benjamin\Desktop\Test\node_modules中,但似乎无法使用它。

C:\Users\Benjamin\Desktop\Test>dir node_modules
 Volume in drive C is OS
 Volume Serial Number is 7294-620F

 Directory of C:\Users\Benjamin\Desktop\Test\node_modules

11/02/2015  10:00 AM    <DIR>          .
11/02/2015  10:00 AM    <DIR>          ..
11/02/2015  10:00 AM    <DIR>          ansi-escapes
11/02/2015  10:00 AM    <DIR>          cli-character-set
11/02/2015  10:00 AM    <DIR>          cli-cursor
11/02/2015  10:00 AM    <DIR>          cli-progress-bar
11/02/2015  10:00 AM    <DIR>          exit-hook
11/02/2015  10:00 AM    <DIR>          lodash.padleft
11/02/2015  10:00 AM    <DIR>          lodash.padright
11/02/2015  10:00 AM    <DIR>          lodash.repeat
11/02/2015  10:00 AM    <DIR>          lodash._basetostring
11/02/2015  10:00 AM    <DIR>          lodash._createpadding
11/02/2015  10:00 AM    <DIR>          log-update
11/02/2015  10:00 AM    <DIR>          onetime
11/02/2015  10:00 AM    <DIR>          restore-cursor
               0 File(s)              0 bytes
              15 Dir(s)  283,532,754,944 bytes free

C:\Users\Benjamin\Desktop\Test>npm ls
Test@1.0.0 C:\Users\Benjamin\Desktop\Test
└─┬ cli-progress-bar@0.1.0
  ├── cli-character-set@0.1.0
  ├─┬ lodash.padleft@3.1.1
  │ ├── lodash._basetostring@3.0.1
  │ └─┬ lodash._createpadding@3.6.1
  │   └── lodash.repeat@3.0.1
  ├── lodash.padright@3.1.1
  └─┬ log-update@1.0.2
    ├── ansi-escapes@1.1.0
    └─┬ cli-cursor@1.0.2
      └─┬ restore-cursor@1.0.1
        ├── exit-hook@1.1.1
        └── onetime@1.0.0

每次我尝试运行脚本时,都会遇到同样的错误。

1 个答案:

答案 0 :(得分:5)

你无法找到名为&#34; cli-progress-bar&#34;是因为该模块没有正确暴露自己。 package.json文件有一个名为main的属性,必须将其设置为从模块导出的脚本的名称。如果脚本被称为index.js,那么npm会找到它,因为脚本名为bar.js,而package.json文件称主文件名为index.js,它没有被正确公开,并且无法通过require找到。