chore: add sample on how to use esm with nodejs experimental flag

This commit is contained in:
Micael Levi L. Cavalcante
2024-10-12 12:23:41 -04:00
parent aff30bf63d
commit e15d73190d
14 changed files with 6423 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};

View File

@@ -0,0 +1,56 @@
# compiled output
/dist
/node_modules
/build
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# temp directory
.temp
.tmp
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

View File

@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}

View File

@@ -0,0 +1,9 @@
## How this works
We are relying on the [`--experimental-require-module`](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require) NodeJS v22 flag so that we can load ESM packages using `require()`
Check out the `package.json` file.
## About automated tests with Jest
While Jest [does not supports](https://github.com/jestjs/jest/issues/15275) the `--experimental-require-module` flag, we cannot use Jest in this project!

View File

@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,49 @@
{
"name": "35-using-esm-package-after-node22",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"type": "commonjs",
"engines": {
"node": ">=22"
},
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start --exec \"node --experimental-require-module\"",
"start:dev": "nest start --exec \"node --experimental-require-module\" --watch",
"start:debug": "nest start --exec \"node --experimental-require-module\" --debug --watch",
"start:prod": "node --experimental-require-module dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
},
"dependencies": {
"@nestjs/common": "10.x",
"@nestjs/core": "10.x",
"@nestjs/platform-express": "10.x",
"reflect-metadata": "0.2.0",
"rxjs": "7.x",
"superjson": "2.x"
},
"devDependencies": {
"@nestjs/cli": "10.x",
"@nestjs/schematics": "10.x",
"@nestjs/testing": "10.x",
"@types/express": "4.x",
"@types/node": "22.x",
"@types/supertest": "6.x",
"@typescript-eslint/eslint-plugin": "8.x",
"@typescript-eslint/parser": "8.x",
"eslint": "8.x",
"eslint-config-prettier": "9.x",
"eslint-plugin-prettier": "5.x",
"prettier": "3.x",
"source-map-support": "0.5.21",
"supertest": "7.x",
"ts-loader": "9.x",
"ts-node": "10.x",
"tsconfig-paths": "4.x",
"typescript": "5.5.x"
}
}

View File

@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello() {
return this.appService.getJsonStringExample();
}
}

View File

@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

View File

@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import superjson from 'superjson';
@Injectable()
export class AppService {
getJsonStringExample() {
const jsonString = superjson.stringify({ big: 10n });
return {
jsonString,
};
}
}

View File

@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

View File

@@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}

View File

@@ -23,6 +23,7 @@ async function executeNpmScriptInSamples(
*/
const minNodejsVersionBySampleNumber = {
'34': 18, // we could use `engines.node` from package.json instead of hardcoding
'35': 22,
};
for await (const dir of directories) {