我正在尝试运行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"
}

最佳答案

试试这个,

npm install --save express-fileupload


希望这行得通。

关于javascript - 尽管安装了npm模块却找不到,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57717467/

10-10 12:40