I've been days trying to get a pure ESM Node(express, graphql) project to run but no success yet, There is a lack of information regarding pure ESM and I give up
What is actually my next milestone is to get the paths working again in the project in the imports statements
What I am getting:
[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): src/**/*
[nodemon] watching extensions: ts,html
[nodemon] starting `./node_modules/.bin/ts-node ./src`
/xxxxxxx/src/server.ts:1
Error: Cannot find module '@routes/api'
Require stack:
- /xxxxxxx/src/server.ts
- /xxxxxxx/src/index.ts
at Object.<anonymous> (/xxxxxxx/src/server.ts:1) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/xxxxxxx/src/server.ts',
'/xxxxxxx/src/index.ts'
]
}
[nodemon] app crashed - waiting for file changes before starting...
My package.json is as follows:
{
"name": "xxxxxxx",
"version": "0.0.0",
"scripts": {
"build": "./node_modules/.bin/ts-node build.ts",
"lint": "eslint . --ext .ts",
"start": "node -r esm -r module-alias/register ./build --env=production",
"start:dev": "nodemon",
"test": "nodemon --config ./spec/nodemon.json",
"test:no-reloading": "./node_modules/.bin/ts-node ./spec"
},
"nodemonConfig": {
"watch": [
"src"
],
"ext": "ts, html",
"ignore": [
"src/public"
],
"exec": "./node_modules/.bin/ts-node ./src"
},
"esm": {
"await": true
},
"_moduleAliases": {
"@services": "build/services",
"@repos": "build/repos",
"@entities": "build/entities",
"@shared": "build/shared",
"@server": "build/server",
"@routes": "build/routes",
"@graphql": "build/graphql"
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"max-len": [
"error",
{
"code": 100
}
],
"no-console": 1,
"no-extra-boolean-cast": 0,
"@typescript-eslint/restrict-plus-operands": 0,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-floating-promises": 0,
"@typescript-eslint/no-unsafe-member-access": 0,
"@typescript-eslint/no-unsafe-assignment": 0
}
},
"eslintIgnore": [
"src/public/",
"build.ts"
],
"dependencies": {
"class-validator": "^0.13.2",
"command-line-args": "^5.2.1",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.1",
"esm": "^3.2.25",
"express": "^4.18.1",
"express-async-errors": "^3.1.1",
"express-graphql": "^0.12.0",
"graphql": "^15.3.0",
"helmet": "^5.1.0",
"http-status-codes": "^2.2.0",
"jet-logger": "^1.1.5",
"jsonfile": "^6.1.0",
"module-alias": "^2.2.2",
"morgan": "^1.10.0",
"pg": "^8.7.3",
"pg-hstore": "^2.3.4",
"reflect-metadata": "^0.1.13",
"sequelize": "^6.21.0",
"type-graphql": "^1.1.1"
},
"devDependencies": {
"@types/command-line-args": "^5.2.0",
"@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.13",
"@types/find": "^0.2.1",
"@types/fs-extra": "^9.0.13",
"@types/jasmine": "^4.0.3",
"@types/jsonfile": "^6.1.0",
"@types/morgan": "^1.9.3",
"@types/node": "^17.0.45",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
"eslint": "^8.18.0",
"find": "^0.3.0",
"fs-extra": "^10.1.0",
"jasmine": "^4.2.1",
"nodemon": "^2.0.16",
"sequelize-cli": "^6.4.1",
"supertest": "^6.2.3",
"ts-node": "^10.8.1",
"tsconfig-paths": "^4.0.0",
"typescript": "^4.7.4"
}
}
and my tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"strict": false,
"moduleResolution": "node",
"baseUrl": "./",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"paths": {
"@config/*": [ "config/*" ],
"@repos/*": [ "src/repos/*" ],
"@models/*": [ "src/models/*" ],
"@shared/*": [ "src/shared/*" ],
"@server": [ "src/server" ],
"@services/*": [ "src/services/*" ],
"@routes/*": [ "src/routes/*" ],
"@graphql/*": [ "src/graphql/*" ]
},
"useUnknownInCatchVariables": false
},
"include": [
"src/**/*.ts",
"spec/**/*.ts"
],
"exclude": [
"src/public/",
"node_modules"
],
"ts-node": {
"esm": true,
"require": ["tsconfig-paths/register", "esm"]
}
}
I need module in ES2022 since I use top level awaits, as you can see I am registering the esm and tsconfig-paths in the ts-node configuration, but I am out of ideas of what should I do to get this finally running, before the problem was "type": "module bacause I was unable to got it running with "esm": true on ts-node with ES2022