Impossible to deploy simple Express application

Hi !

I’m trying to deploy a simple Express application on Layer0, however after I deployed the application, the server doesn’t seem to be started. When I start locally the application with the layer0 dev command, I get an error:

$ npm run layer0:dev

> test-express@1.0.0 layer0:dev D:\Code\test-express
> layer0 dev

> Starting Layer0 in development mode with caching disabled...
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\Code\test-express\node_modules\chalk\source\index.js
require() of ES modules is not supported.
require() of D:\Code\test-express\node_modules\chalk\source\index.js from D:\Code\test-express\node_modules\@layer0\express\dev.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from D:\Code\test-express\node_modules\chalk\package.json.

    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (D:\Code\test-express\node_modules\@layer0\express\dev.js:10:33)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32) {
  code: 'ERR_REQUIRE_ESM'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 4294967295
npm ERR! test-express@1.0.0 layer0:dev: `layer0 dev`
npm ERR! Exit status 4294967295
npm ERR!
npm ERR! Failed at the test-express@1.0.0 layer0:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\me\AppData\Roaming\npm-cache\_logs\2022-03-08T15_55_29_504Z-debug.log

Here is my package.json:

{
  "scripts": {
    "layer0:dev": "layer0 dev",
    "layer0:build": "layer0 build",
    "layer0:deploy": "layer0 deploy",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "name": "test-express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@layer0/express": "^4.10.3",
    "express": "^4.17.3"
  },
  "devDependencies": {
    "@layer0/cli": "^4.10.3",
    "@layer0/core": "^4.10.3",
    "@layer0/devtools": "^4.10.3",
    "@layer0/prefetch": "^4.10.3"
  }
}

Here is my index.js:

const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

My node version is v14.18.3

Thank you for your help !

It looks like our @layer0/express connector may be using a version of chalk that is strictly ESM. I have created a build which uses 4.1.2 to be compatible. Can you try upgrading your app to this pre-release to verify the fix?

Simply run npx layer0 use 4.10.4-next-1646762372-bf262e465.0

1 Like

I ran my app with your pre-release and it indeed worked locally.

Thank you!

Great to hear! This fix will be available in our next @layer release.

1 Like