I have been trying to import an ESM module written in typescript in nodejs. But I am getting the following error:
An import path cannot end with a '.ts' extension.
Util.ts
export class Util {
constructor ( ) {
}
log(msg) {
console.log(msg)
}
}
index.ts
import {log} from './Util.ts'
log(task.query.criteria, payload.parameters)
I have also added "type":"module" inside package.json
I changes .ts to .js just to see if it works and then I got :
Object.defineProperty(exports, "__esModule", { value: true }); ^
ReferenceError: exports is not defined
at file:///C:/Users/abc/NestJsPOC/NestPOC/dist/main.js:2:23
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
EDIT
I have also tried:
var log = require('../utility/util.js');
Util.js
function log(msg) {
console.log(msg)
}
module.exports= { log}
index.ts
log('hello')
Error:
TypeError: log is not a function