I’m starting a new Node.js project with Typescript. This works fine:
server.ts
const express = require("express")
const app = express()
app.listen(3000, () => {
console.log("Server running")
})
But this was before using Typescript, for this project I use this configuration:
import express from 'express'
const app = express()
app.listen(3000, () => {
console.log("Server running")
})
But the app crashes, it gives the error message:
SyntaxError: Cannot use import statement outside a module
In tsconfig.json, I just changed the lines:
"target": "es2021",
..................
"outDir": "./build",
The package.json scripts:
"scripts": {
"dev": "node --env-file=.env --watch server.ts",
"build": "tsc",
"start": "node build/server.js"
},
So what could be the problem?