Dynamic import()
with "module": "commonjs"
#1290
-
With Here are a few options to do that: Option one: use
|
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 14 replies
-
This is a recipe for solving a common problem. We'll try to keep it up-to-date. If it is confusing, we can update it based on questions posted here. |
Beta Was this translation helpful? Give feedback.
-
This might be due to my inexperience with Typescript but this seems to be the challenge:
I couldn't figure out a way to force Typescript not to compile a file that is included by a TS file. Even if it's a |
Beta Was this translation helpful? Give feedback.
-
Code seems invalid:
Changed to but getting:
Added
Changed to
Note: tsconfig:
Trial #2 Changed to
can't find the file now. Enabled js in tsconfig:
Back to square 1. - This is where I started. I guess this can't be used anymore. It's really insane how hard it to use ESM module with ts-node. Wasted hours trying random stuff... and still can't find a solution. |
Beta Was this translation helpful? Give feedback.
-
with my own package I can just import types and real fn in a cleaner way:
The benefit of esm is minimal, but having to change ton of stuff is too expensive. I rather drop the library or re-implement myself at this point. |
Beta Was this translation helpful? Give feedback.
-
Use moduleResolution: 'node16' with module: 'commonjs' in tsconfig.json, ts will keep all the dynamic import statements as is. But ts-node will panic at this situation. |
Beta Was this translation helpful? Give feedback.
-
This one helped me fix my problem in a case where external packages in a project are limited. Thanks a lot 👍 |
Beta Was this translation helpful? Give feedback.
-
Note for "Option two: copy-paste this helper function" that on Windows we need to use |
Beta Was this translation helpful? Give feedback.
-
Ok, I came across this approach for resolving typings. My dynamic import: import { basename } from 'path';
const defaultOrName = (path: string) => path.endsWith('index.js') ? 'default' : basename(path).replace('.js', '');
// we use this to import magidoc module because it's an ESM module and direct
// imports won't work...
// see: https://github.com/TypeStrong/ts-node/discussions/1290
const _import = async (path: string) => new Function('specifier', 'return import(specifier)')(path);
// we create a wrapper for the _import above and returns the `default` exported method/function/symbol
// inferred by the module name or a custom one base on user preferences...
export const dynamicImport = async <T>(path: string, symbol?: string) => (await _import(path))[symbol ?? defaultOrName(path)] as T; Then, I import/re-export types like the following -- in my case, I'm trying to import magidoc ESM into a commonjs app --: // imports
import type { default as generate } from '@magidoc/cli/build/commands/generate/index';
import type { withStacktrace } from '@magidoc/cli/build/commands/utils/withStacktrace';
import type { readConfiguration } from '@magidoc/cli/build/config/reader';
import type { MagidocConfiguration } from '@magidoc/cli/build/config/types';
// exports
export type { MagidocConfiguration };
export type { GenerationConfig } from '@magidoc/cli/build/commands/generate/index';
export type GenerateFunction = typeof generate;
export type withStacktraceFunction = typeof withStacktrace;
export type readConfigurationFunction = typeof readConfiguration;
export type GenerateWrapper = (config: MagidocConfiguration) => Promise<void>; And then I use it like: const readConfiguration = await dynamicImport<readConfigurationFunction>('@magidoc/cli/build/config/reader.js', 'readConfiguration');
const withStacktrace = await dynamicImport<withStacktraceFunction>('@magidoc/cli/build/commands/utils/withStacktrace.js')
const generate = await dynamicImport<GenerateFunction>('@magidoc/cli/build/commands/generate/index.js') This allows me to have typings while coding and it also works. |
Beta Was this translation helpful? Give feedback.
-
Can use jiti instead of tsimportlib. (Weekly Downloads: 12M) |
Beta Was this translation helpful? Give feedback.
-
How do I use all what you guys are saying with my tsconfig and package .json so I deploy the code to docker but first I want to npm run build for it not to have problems this was currently what I got \dist\cjs\config\utils\cookies.js not supported. \dist\cjs\config\utils\cookies.js to a dynamic import() which is available in all CommonJS modules |
Beta Was this translation helpful? Give feedback.
This is a recipe for solving a common problem. We'll try to keep it up-to-date. If it is confusing, we can update it based on questions posted here.