0

Using example at express-fileupload Examples

<pre>   
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('/somewhere/on/your/server/filename.jpg', function(err) {
 if (err)
 return res.status(500).send(err);

 res.send('File uploaded!');
 });
 }); <code>

I get this error

nodejs server1.js /var/www/html/express/server1.js:14 let sampleFile = req.files.sampleFile; ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

Something simple I'm sure. Even pasting the code here the 'let' statement is isolated.

  • You're using a relatively old version of Node.js. Try updating it, or add this as first line to your script: `'use strict';` (including the quotes) – robertklep Nov 29 '18 at 14:48
  • Possible duplicate of [Block-scoped declarations not yet supported outside strict mode](https://stackoverflow.com/questions/41960142/block-scoped-declarations-not-yet-supported-outside-strict-mode) – SanSolo Nov 29 '18 at 14:51
  • That worked. I'm using nodejs -v v4.8.2. Unfortunately now I'm getting Error: Cannot find module 'express-fileupload' Why do these developer examples fail so frequently. – user8796129 Nov 29 '18 at 14:53
  • I finally got a good update. # Using Debian, as root curl -sL https://deb.nodesource.com/setup_10.x | bash - apt-get install -y nodejs – user8796129 Nov 29 '18 at 14:57

1 Answers1

0

On the block-scoped declaration, the let keyword tells NodeJS that the variable you are declaring will only exist within the innermost control block of code. This may be a function or a set of curly braces within a function, most frequently seen in loops. It is not supported in early versions of node. Look into the nvm tool to learn how to switch between different versions of node as necessary. Usually you will want to use the latest long-term-support version.

On cannot find module, you are looking for the npm tool, which is used to install node modules. It cannot find express-fileupload, so you want to install that file from npm. You can install the module with:

npm install express-fileupload

Or with the shorthand npm i express-fileupload

If you happen to be using a really old version of npm, it is better practice to use

npm i express-fileupload --save

This will store the face that your project is dependent on the express-fileupload package in a file called package.json, so that npm knows this later for dependency management such as re-installation of packages, auditing, or installing dependencies when you deploy to other systems. Newer versions of npm do this automatically. You would use npm i express-fileupload --save-dev if you only cared about this dependency in a development environment, and not in a production environment.

Tom
  • 939
  • 5
  • 9