Compile as ESM from TS using ncc #2394
-
I've been attempting to compile a Node.js project written in TypeScript using ncc.
From what I can tell, this error is a result of references to the I understand that this is not necessarily a concern for this package, but I'm curious if anyone has a ideas for how to workaround this issue. I have the following code from the documentation: const config = createConfig({
http: {
listen: 8090, // port, UNIX socket or options
},
cors: false,
});
const helloWorldEndpoint = defaultEndpointsFactory.build({
// method: "get" (default) or array ["get", "post", ...]
input: z.object({
name: z.string().optional(),
}),
output: z.object({
greetings: z.string(),
}),
handler: async ({ input: { name }, options, logger }) => {
logger.debug("Options:", options); // middlewares provide options
return { greetings: `Hello, ${name || "World"}. Happy coding!` };
},
});
const routing: Routing = {
v1: {
hello: helloWorldEndpoint,
},
};
await createServer(config, routing) This is my tsconfig.json {
"compilerOptions": {
"lib": [],
"moduleResolution": "bundler",
"module": "esnext",
"target": "esnext",
"isolatedModules": true,
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
},
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
After fiddling some more, setting |
Beta Was this translation helpful? Give feedback.
-
which file is it about, @nkooman-bzs ? |
Beta Was this translation helpful? Give feedback.
Then the message you get is caused by the combination of three reasons, @nkooman-bzs :
__filename
is the CJS shorthand which is not present in ESMncc
that bundles everything, includingtypescript
, that contains__filename
, andncc
does not transform it accordinglypackage.json
hastype: "module"
entry..\dist\package.json
, so check it outMy advice:
tsup
for transforming your Typescript code, ortsx
for running your Typescript code;ncc
documentation on how either:typescript