簡體   English   中英

盡管安裝了npm模塊卻找不到

[英]can't find npm module despite installing it

我正在嘗試運行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

希望這行得通。

您進行了所謂的全局安裝,即在系統級別安裝模塊,該模塊對於諸如命令行實用程序之類的操作很有用。

既然你想require一個模塊,你需要做的是本地安裝,這使模塊進入node_modules ,你的應用程序可以require它。

代替

npm install -g express-fileupload

采用

npm install express-fileupload --save

-g是全球指標。 刪除它。 我還添加了--save ,這將express-fileupload添加到package.json dependencies ,這對於以后能夠重現此配置很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM