mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
chore: add sample on how to use esm with nodejs experimental flag
This commit is contained in:
25
sample/35-use-esm-package-after-node22/.eslintrc.js
Normal file
25
sample/35-use-esm-package-after-node22/.eslintrc.js
Normal 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',
|
||||
},
|
||||
};
|
||||
56
sample/35-use-esm-package-after-node22/.gitignore
vendored
Normal file
56
sample/35-use-esm-package-after-node22/.gitignore
vendored
Normal 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
|
||||
4
sample/35-use-esm-package-after-node22/.prettierrc
Normal file
4
sample/35-use-esm-package-after-node22/.prettierrc
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
9
sample/35-use-esm-package-after-node22/README.md
Normal file
9
sample/35-use-esm-package-after-node22/README.md
Normal 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!
|
||||
8
sample/35-use-esm-package-after-node22/nest-cli.json
Normal file
8
sample/35-use-esm-package-after-node22/nest-cli.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
}
|
||||
}
|
||||
6203
sample/35-use-esm-package-after-node22/package-lock.json
generated
Normal file
6203
sample/35-use-esm-package-after-node22/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
49
sample/35-use-esm-package-after-node22/package.json
Normal file
49
sample/35-use-esm-package-after-node22/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
12
sample/35-use-esm-package-after-node22/src/app.controller.ts
Normal file
12
sample/35-use-esm-package-after-node22/src/app.controller.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
10
sample/35-use-esm-package-after-node22/src/app.module.ts
Normal file
10
sample/35-use-esm-package-after-node22/src/app.module.ts
Normal 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 {}
|
||||
13
sample/35-use-esm-package-after-node22/src/app.service.ts
Normal file
13
sample/35-use-esm-package-after-node22/src/app.service.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
8
sample/35-use-esm-package-after-node22/src/main.ts
Normal file
8
sample/35-use-esm-package-after-node22/src/main.ts
Normal 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();
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
|
||||
}
|
||||
21
sample/35-use-esm-package-after-node22/tsconfig.json
Normal file
21
sample/35-use-esm-package-after-node22/tsconfig.json
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user