Error [ERR_REQUIRE_ESM]: require() of ES Module #735
-
Hello, guys. I've been struggling with this error for around one hour. Would you mind to help me with some guidance? I know there are some compatibility issues, but I would have taught that I'm getting the following error:
Here you can see my {
"name": "ta-rest_api",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/app.js",
"dev": "nodemon src/app.ts",
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@binance/connector-typescript": "^0.3.14",
"agenda": "^5.0.0",
"dotenv": "^16.4.7",
"express": "^4.21.1",
"mongoose": "^8.8.3",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"trading-signals": "^5.0.4",
"winston": "^3.17.0"
},
"devDependencies": {
"@types/express": "^5.0.0",
"@types/node": "^22.10.1",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.7",
"@types/winston": "^2.4.4",
"nodemon": "^3.1.7",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
}
} And my {
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
} I've also tried some (70%), but not all, of this AI generated solutions: https://chatgpt.com/share/675e4c50-7fe0-800b-9426-1d1f34a8858f (I'll be covering all possible AI approaches next week). Please, help me out with this if you have some work around. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @carloswm85, thanks for asking and trying to find a solution. Your main problem is that my code is written with ECMAScript Module syntax but your project uses CommonJS. Here is how to fix it: #663 (comment) Hope this helps! Best, |
Beta Was this translation helpful? Give feedback.
-
Well, @bennycode, I was able to fix it, hopefully in the right way, but using a different solution (your previous answer led in the right direction in the long run, thank you for that). Following these instructions https://www.npmjs.com/package/@tsconfig/node20 for MY node environment
npm install --save-dev @tsconfig/node20
{
"$schema": "https://json.schemastore.org/tsconfig",
"_version": "20.1.0",
"compilerOptions": {
"lib": [
"es2022"
],
"module": "node16",
"target": "es2022",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node16",
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
} Look here https://github.com/tsconfig/bases#centralized-recommendations-for-tsconfig-bases for your specific environment.
// MY SERVICE
export class BinanceService {
// SOME CODE...
// This is a private method
async getEmacData(arrayOfValues) {
// ⚠️ The important lines for our current problem ⚠️
const tradingSignals = await import('trading-signals');
const { EMA } = tradingSignals;
// MORE CODE
const length = 9;
const open = new EMA(length);
open.updates(arrayOfValues);
const result = open.getResult().toNumber();
return result;
}
} So far, the results seem to be the right ones: But I'm not happy with this solution. I'd rather prefer to import it like: import { Interval, Spot } from "@binance/connector-typescript"; How can I achieve that? PS: I'm rather new to TypeScript development. |
Beta Was this translation helpful? Give feedback.
-
@carloswm85 I have some great news for you! With trading-signals v6 you can import the package also as CJS module: const {Big, SMA} = require('trading-signals'); |
Beta Was this translation helpful? Give feedback.
Well, @bennycode, I was able to fix it, hopefully in the right way, but using a different solution (your previous answer led in the right direction in the long run, thank you for that).
Following these instructions https://www.npmjs.com/package/@tsconfig/node20 for MY node environment
v20.9.0
:npm install --save-dev @tsconfig/node20
tsconfig.json
: