Issue compile static JS

Dear fork,

I deploy a simple express.js to vercel but it does not work.

index.js

const express = require('express');

const app = express();


const path = require('path')
app.use(express.static(path.join(__dirname, '../static')));
app.get('/*', function(req, res){
  res.sendFile("index.html", {root: path.join(__dirname, '../static')});
});


app.get('/api', (req, res) => {
  res.send('Hello from Express.js!');
});


const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

In static folder, i have some JS files, the vercel build add some lines into the file ~~

Object.defineProperty(exports, "__esModule", {
  value: true
});

I try to add type: "module" in package.json but all server will die.

Log:

Running "vercel build"
Vercel CLI 35.2.1
WARN! Due to `builds` existing in your configuration file, the Build and Development Settings defined in your Project Settings will not apply. Learn More: https://vercel.link/unused-build-settings
Installing dependencies...
up to date in 1s
12 packages are looking for funding
  run `npm fund` for details
Warning: Node.js functions are compiled from ESM to CommonJS. If this is not intended, add "type": "module" to your package.json file.
Compiling "index.js" from ESM to CommonJS...
Compiling "bootstrap.esm.min.js" from ESM to CommonJS...
Compiling "bootstrap.esm.js" from ESM to CommonJS...
Compiling "start.js" from ESM to CommonJS...
Build Completed in /vercel/output [1m]
Deploying outputs...

I want to compile only “index.js” from ESM to CommonJS. But vercel compile all file ~~

vercel.json

{
  "version": 2,
  "cleanUrls": true,
  "framework": null,
  "installCommand": "npm install",
  "outputDirectory": ".",
  "public": false,
  "builds": [
    {
      "src": "/src/index.js",
      "use": "@vercel/node"
    }
  ],
  "rewrites": [{ "source": "/(.*)", "destination": "/src/index.js" }]
}

The same issue but no answer: don't compile static js · vercel · Discussion #3168 · GitHub
I think is a vercel bug?

Please help me ~~ there is no document for this issue.
Many thanks

Hi @sodepr-deploy. Have you tried the Express.js with Vercel guide? It has a walkthrough of recommended project setup and some tips about adapting Express to serverless environments.

Thank for you reply,

However, I cannot check, my account is paused because someone ddos my site ~~

image

Does vercel has any waf to protect api/website?

Sorry to hear your site is experiencing unexpectedly high usage. You can learn more about Vercel Firewall and DDoS mitigation here:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.