0

I am trying to run this script on node and it requires apart from standard library the module : multiparty

var multiparty = require('multiparty');
var http = require('http');
var util = require('util');

http.createServer(function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, { 'content-type': 'text/plain' });
      res.write('received upload:\n\n');
      res.end(util.inspect({ fields: fields, files: files }));
    });

    return;
  }

  // show a file upload form
  res.writeHead(200, { 'content-type': 'text/html' });
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);

The script i am using is an official example given by the multiparty documentation. Since i am running this script in REPL mode of node, i decided to first install multiparty globally with this command

npm install -g multiparty

The module was installed and after that i tried to run it on node but i get the following error when uploading the file i.e. localhost:8080/upload

Uncaught TypeError : Cannot read property 'Form' of undefined

and also a Module Not found error as seen below enter image description here

I also tried installing multiparty with following command

npm install multiparty

But the error still persists.

Adnan Ahmed
  • 466
  • 1
  • 6
  • 15

1 Answers1

0

I looked into the answers in NPM global install "cannot find module" and realized that my environment variable NODE_PATH was not set and it should be set to C:\Users\%USER_PROFILE%\AppData\Roaming\npm\node_modules

Adnan Ahmed
  • 466
  • 1
  • 6
  • 15