Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commonjs support for typescript environment. #85

Open
douglas-wang opened this issue Aug 17, 2022 · 1 comment
Open

Commonjs support for typescript environment. #85

douglas-wang opened this issue Aug 17, 2022 · 1 comment

Comments

@douglas-wang
Copy link

@mike-marcacci , is there any common js support?

I am trying to use fs-capacitor in the following way and I am getting this weird error now.

import { ReadStream, ReadStreamOptions, WriteStream } from 'fs-capacitor'
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/fs-capacitor/dist/index.js from .../server.ts not supported.
Instead change the require of index.js in .../server.ts to a dynamic import() which is available in all CommonJS modules.
...

I am using ts-node/ts-node-dev for the development and here is the tsconfig.json file.

{
  "compilerOptions": {
    "module": "commonjs",
    "incremental": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "target": "ES2018",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "skipLibCheck": true,
    "outDir": "dist",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "strict": true,
    "resolveJsonModule": true,
    "removeComments": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "rootDir": "."
  },
  "include": ["src"],
  "exclude": ["node_modules/**", "dist"]
}

I am also using "module": "commonjs" in package.json.

@jaydenseric
Copy link
Collaborator

A side note, but you should look at the TS config compilerOptions module and moduleResolution option values you are using; they are outdated. There are "nodenext" and "node16" values which work much better with the way Node.js ESM, package exports, etc. works.

That said, the simple answer is that if your module is CJS then your can't require an ESM module; that's just a limitation of the way CJS and ESM works. ESM can import CJS, but not the other way around (with the exception of dynamic imports). Your source code might look like it's ESM, but if you're compiling it to CJS modules and that's what runs then you will hit problems like this.

The best thing to do is to migrate your project to using proper ESM at runtime; something Node.js is perfectly capable of. Then you can import ESM modules like those exported by fs-capacitor without problems. If you are using TypeScript to compile your source code you should use the file extension .mts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants