Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_REQUIRE_ESM error when running application inside Docker #1229

Open
samsurya336 opened this issue Aug 4, 2024 Discussed in #1228 · 0 comments
Open

ERR_REQUIRE_ESM error when running application inside Docker #1229

samsurya336 opened this issue Aug 4, 2024 Discussed in #1228 · 0 comments

Comments

@samsurya336
Copy link

Discussed in #1228

Originally posted by samsurya336 August 3, 2024
Hi everyone, I followed this repo to setup nodemailer for my NestJS application.
And I did

yarn add @nestjs-modules/mailer nodemailer
yarn add -D @types/nodemailer
yarn add ejs

And the application starts fine locally, no issues. Running as expected on my local machine.

But the error occurs when we try to run the application via Docker.

Note: Both my local machine & Docker image contains the same node & yarn version I cross checked it

Local machine:
OS: macOS sonoma
node: 18.20.3
yarn: 1.22.19

Below is the Error:

pro-academy-core-backend % docker-compose -f docker-compose.dev.yml up --attach pro-academy-core-backend
[+] Building 0.0s (0/0)                                                                                                                                                                                                                    docker:desktop-linux
[+] Running 2/2
 ✔ Container pro-academy-core-backend-pro-academy-core-mongo-1  Created                                                                                                                                                                                    0.0s 
 ✔ Container pro_academy_core_backend                           Created                                                                                                                                                                                   19.5s 
Attaching to pro_academy_core_backend
pro_academy_core_backend  | 
pro_academy_core_backend  | > [email protected] start:dev
pro_academy_core_backend  | > nest start --watch
pro_academy_core_backend  | 
pro_academy_core_backend  | /usr/app/node_modules/strip-ansi/index.js:2
pro_academy_core_backend  | const ansiRegex = require('ansi-regex');
pro_academy_core_backend  |                   ^
pro_academy_core_backend  | 
pro_academy_core_backend  | Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/app/node_modules/strip-ansi/node_modules/ansi-regex/index.js from /usr/app/node_modules/strip-ansi/index.js not supported.
pro_academy_core_backend  | Instead change the require of /usr/app/node_modules/strip-ansi/node_modules/ansi-regex/index.js in /usr/app/node_modules/strip-ansi/index.js to a dynamic import() which is available in all CommonJS modules.
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/strip-ansi/index.js:2:19)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/string-width/index.js:2:19)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/wrap-ansi/index.js:2:21)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/inquirer/lib/utils/screen-manager.js:4:18)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/inquirer/lib/prompts/base.js:14:23)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/inquirer/lib/prompts/list.js:11:14)
pro_academy_core_backend  |     at promptModule.restoreDefaultPrompts (/usr/app/node_modules/inquirer/lib/inquirer.js:61:33)
pro_academy_core_backend  |     at inquirer.createPromptModule (/usr/app/node_modules/inquirer/lib/inquirer.js:72:16)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/inquirer/lib/inquirer.js:84:28)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/@nestjs/cli/lib/utils/project-utils.js:4:18)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/@nestjs/cli/actions/generate.action.js:9:25)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/@nestjs/cli/actions/index.js:19:14)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/@nestjs/cli/commands/command.loader.js:5:19)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/@nestjs/cli/commands/index.js:17:14)
pro_academy_core_backend  |     at Object.<anonymous> (/usr/app/node_modules/@nestjs/cli/bin/nest.js:5:20) {
pro_academy_core_backend  |   code: 'ERR_REQUIRE_ESM'
pro_academy_core_backend  | }
pro_academy_core_backend  | 
pro_academy_core_backend  | Node.js v18.20.3
pro_academy_core_backend exited with code 1

Dockerfile.dev

# Using a Debian build
FROM node:18.20.3-alpine
RUN mkdir -p /usr/app
WORKDIR /usr/app

# Install Dependencies
COPY ./package*.json ./
COPY ./yarn.lock ./
RUN npm i -g @nestjs/cli
RUN npm install -g [email protected]
RUN yarn install --frozen-lockfile

# Copy app source code
COPY . .

# Ports
EXPOSE 9000
EXPOSE 8080
EXPOSE 9229

# Run npm start when container starts
CMD [ "yarn", "start:dev"]

package.json

{
  "name": "pro-academy-core-backend",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "type:check": "tsc",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@faker-js/faker": "^8.3.1",
    "@nestjs-modules/mailer": "^2.0.2",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.0.0",
    "@nestjs/jwt": "^10.2.0",
    "@nestjs/mongoose": "^10.0.2",
    "@nestjs/passport": "^10.0.3",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/swagger": "^7.1.15",
    "@nestjs/terminus": "^10.2.0",
    "@nestjs/throttler": "^5.1.1",
    "@types/passport-jwt": "^4.0.1",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "crypto-js": "^4.2.0",
    "dotenv": "^16.3.1",
    "faker": "^6.6.6",
    "helmet": "^7.1.0",
    "json-stringify-safe": "^5.0.1",
    "jsonwebtoken": "^9.0.2",
    "lodash": "^4.17.21",
    "moment": "^2.29.4",
    "moment-timezone": "^0.5.43",
    "mongoose": "^8.0.0",
    "mongoose-autopopulate": "^1.1.0",
    "nanoid": "3.3.4",
    "nodemailer": "^6.9.14",
    "passport": "^0.7.0",
    "passport-jwt": "^4.0.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1",
    "winston": "^3.11.0",
    "winston-daily-rotate-file": "^4.7.1"
  },
  "devDependencies": {
    "@compodoc/compodoc": "^1.1.23",
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.2.8",
    "@types/bcryptjs": "^2.4.6",
    "@types/crypto-js": "^4.2.1",
    "@types/express": "^4.17.17",
    "@types/faker": "^6.6.9",
    "@types/jest": "^29.5.2",
    "@types/json-stringify-safe": "^5.0.3",
    "@types/jsonwebtoken": "^9.0.5",
    "@types/lodash": "^4.14.201",
    "@types/multer": "^1.4.11",
    "@types/nanoid": "^3.0.0",
    "@types/node": "^20.3.1",
    "@types/nodemailer": "^6.4.15",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "concurrently": "^8.2.2",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,

    "esModuleInterop": true,
    
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,

    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "paths": {
      "@test/*": ["test/*"],
      "@configs/*": ["src/shared/configs/*"],
      "@decorators/*": ["src/shared/decorators/*"],
      "@guards/*": ["src/shared/guards/*"],
      "@apis/*": ["src/apis/*"],
      "@shared/*": ["src/shared/*"],
      "@root/*": ["src/*"],
    }
  }
}

Docker only throw error after I installed

yarn add @nestjs-modules/mailer nodemailer
yarn add -D @types/nodemailer
yarn add ejs

When I remove those packages & Build my image & then run it. No Error occurs.

What could be the possible issue, could anyone help we with this.
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant