-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
nodenext, commonjs, transpileModule, module, esm
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about nodenext
β― Playground Link
https://stackblitz.com/edit/wpaw3gcw?file=build.js
π» Code
index.ts
export function test() {
console.log('test')
}build.js
import * as ts from 'typescript';
import { basename, dirname, resolve } from 'node:path';
import { readFileSync } from 'node:fs';
const fileName = resolve('./index.ts');
const tsconfigPath = ts.findConfigFile(fileName, ts.sys.fileExists);
const { config, error } = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
if (error) {
throw error;
}
const compilerOptions = ts.parseJsonConfigFileContent(
config,
ts.sys,
dirname(tsconfigPath),
undefined,
basename(tsconfigPath)
).options;
const transpileResult = ts.transpileModule(readFileSync(fileName, 'utf8'), {
compilerOptions,
fileName,
});
// Should not contain require or exports
console.log(transpileResult);
tsconfig.json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"target": "ES2022",
"module": "nodenext"
}
}π Actual behavior
When transpiling a file with ts.transpileModule it is transpiled to commonjs even though "type": "module" is configured in the related package.json
π Expected behavior
Transpiling a file with ts.transpileModule should transpile it to esm with "type": "module" configured in the related package.json
Additional information about the issue
From an initial analysis, this seems to be because in the transpile phase, the provided host cannot read the package.json, as the fileExists from the host will return false for an existing package.json.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created