seangwright / typescript-functional-extensions

A TypeScript implementation of the C# library CSharpFunctionalExtensions, including synchronous and asynchronous Maybe and Result monads.
MIT License
34 stars 4 forks source link

NestJS app does not compile when typescript-functional-extensions package is referenced #3

Closed xShivan closed 2 years ago

xShivan commented 2 years ago

Hello.

First I'd like to say that I really appreciate the hard work you've put into the library. I'm coming from the C# world into the TypeScript and now I can use my favorite library in TypeScript!

I'm currently developing applications using NestJS. When I try to reference the library it throws me compilation errors like below.

When I copy source code of the library into my project everything runs just fine.

[8:52:59 AM] File change detected. Starting incremental compilation...

[8:52:59 AM] Found 0 errors. Watching for file changes.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/michal/dev/test/node_modules/typescript-functional-extensions/dist/index.js
require() of ES modules is not supported.
require() of /Users/michal/dev/test/node_modules/typescript-functional-extensions/dist/index.js from /Users/michal/dev/test/dist/app.service.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/michal/dev/test/node_modules/typescript-functional-extensions/package.json.

    at new NodeError (internal/errors.js:322:7)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1102:13)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.<anonymous> (/Users/michal/dev/test/src/app.service.ts:2:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)

This is a sample service where I use it:

import { Injectable } from '@nestjs/common';
import { Result } from 'typescript-functional-extensions';

@Injectable()
export class AppService {
  getHello() {
    return Result.success().map(() => 'test');
  }
}

To Reproduce Steps to reproduce the behavior:

  1. Reference typescript-functional-extensions in NestJS project.
  2. Run start or start:dev script.
  3. Error like above should be present in console.

Expected behavior App should run successfully.

seangwright commented 2 years ago

Ah, this is probably related to how I'm building the library. I create a native ES Module package for this library, which can't be used with the require() syntax in Node.

You can see the target for output for the TypeScript compilation.

I believe this is related to https://github.com/nestjs/nest/issues/7021

So either Nest needs to be used with a bundler/build process to convert the ES Module to CommonJs to be used with Nest, or I need to compile this library to CommonJs.

I'm currently using this library in multiple Vue projects (bundled with Webpack and Vite) and it works fine. Also, if someone is using modern JS and ES Modules natively, they can use this library without needing a bundler.

I could explore potentially creating 2 packages or including a CommonJs build in the package along side the ES Module build, but I'd prefer to always ship ES Modules in the package vs creating a legacy package.

seangwright commented 2 years ago

Try installing https://www.npmjs.com/package/typescript-functional-extensions/v/1.0.1-alpha.1

I added dual ES Module / CommonJs support in a branch.

I'll need to test it on my projects, but if it works for both of us, I'll publish it as 1.0.1.

xShivan commented 2 years ago

Thank you! It works like a charm with Nest projects! From my point of view it's ready to be published.

seangwright commented 2 years ago

Glad it works. I'm still testing on my end and I'm working on #4.

I should be able to get that 1.0.1 out in the next day or two.

seangwright commented 2 years ago

Published as v1.0.1