尽管安装了npm模块却找不到

时间:2019-08-29 20:38:11

标签: javascript node.js

我正在尝试运行express-fileupload的示例:

const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();

// default options
app.use(fileUpload());

app.post('/upload', function(req, res) {
  if (Object.keys(req.files).length == 0) {
    return res.status(400).send('No files were uploaded.');
  }

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  let sampleFile = req.files.sampleFile;

  // Use the mv() method to place the file somewhere on your server
  sampleFile.mv('/filename.jpg', function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');
  });
});
  app.listen(2000)

尽管使用以下命令安装“成功”模块:

npm install -g express-fileupload 

我收到以下错误:

    internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'express-fileupload'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\Users\SESA476712\Desktop\delete\dragand drop\app-js:2:20)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

知道我在这里缺少什么吗? 提前致谢!

第一次更新 package.json:

{
  "name": "draganddrop",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

2 个答案:

答案 0 :(得分:1)

尝试一下,

npm install --save express-fileupload

希望这行得通。

答案 1 :(得分:1)

您进行了所谓的全局安装,该安装在系统级别安装模块,对命令行实用程序等有用。

由于您要尝试require模块,因此您需要做的是本地安装,该安装会将模块放入node_modules,应用程序可以在其中进行require

代替

npm install -g express-fileupload

使用

npm install express-fileupload --save

-g是全球指标。删除它。我还添加了--save,将express-fileupload添加到您的package.json dependencies中,这对您以后能够重现此配置很重要。