Yes, you can use both. Here's info to help you reproduce a successful test:
Node.js (LTS) version:
$ node --version
v16.14.0
./package.json
:
{
"name": "so-71099311",
"version": "0.1.0",
"private": true,
"description": "",
"type": "module",
"scripts": {
"compile": "tsc",
"start": "npm run compile && node dist/main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@types/node": "^17.0.17",
"typescript": "^4.5.5"
}
}
./tsconfig.json
:
{
"compilerOptions": {
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"isolatedModules": true,
"lib": [
"esnext"
],
"module": "esnext",
"moduleResolution": "node",
"noUncheckedIndexedAccess": true,
"outDir": "dist",
"strict": true,
"target": "esnext",
"useUnknownInCatchVariables": true
},
"include": [
"./src/**/*"
]
}
./src/module.ts
:
export function greet (name = 'world'): void {
console.log(`Hello ${name}`);
}
./src/main.ts
:
import {greet} from './module.js';
const name = await Promise.resolve('Nguyen');
greet(name);
In your console:
$ cd /path/to/the/directory/where/you/created/these/files
$ npm install
$ npm run start
> [email protected] start
> npm run compile && node dist/main.js
> [email protected] compile
> tsc
Hello Nguyen
'./module.js'
but it is'./module.ts'
. And you can't change the import to end on.ts
because that's not allowed. You also can't remove the.js
entirely because of"type": "module"
. It's a catch-22. – Whiffle