Search code examples
javascriptnode.jsexpressvercel

Vercel + Express.js don't serve JS files


I deployed an Express.js server on Vercel (using NowCLI), in my index.js file, I made that every file in my dash/assets folder are served at assetsDash/ using app.use('/assetsDash', express.static(path.join(__dirname, 'dash', 'assets'))) and this works, but .js files are not served (btw this work on localhost, but not with Vercel).

index.js

// Import FS and Path
const fs = require('fs')
const path = require('path')

// Prepare an web server using express.js
const express = require('express')
const app = express()

// Add pages to the website
    // Assets
    app.use('/assetsLanding', express.static(path.join(__dirname, 'landing', 'assets')))
    app.use('/assetsDash', express.static(path.join(__dirname, 'dash', 'assets')))

// Start the web server
const server = app.listen(process.env.PORT || 3000, () => {
    console.log(`Started on ${server.address().port} port`);
});

now.json

{
    "version": 2,
    "builds": [{
      "src": "./index.js",
      "use": "@now/node-server"
    }],
    "routes": [{"handle": "filesystem"},
      {
        "src": "/.*",
        "dest": "index.js"
      }
    ]
  }

Solution

  • Edit : I managed to solve it by replacing NowCLI by VercelCLI : install @vercel/node through NPM, then renaming now.json file by vercel.json and by replacing a line in the file.

    "builds": [{
          "src": "./index.js",
    -     "use": "@now/node-server"
    +     "use": "@vercel/node"
    }],