Skip to content

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

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

Closed
xShivan opened this issue Dec 28, 2021 · 5 comments · Fixed by #5
Closed

Comments

@xShivan
Copy link

xShivan commented Dec 28, 2021

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
Copy link
Owner

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 nestjs/nest#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
Copy link
Owner

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
Copy link
Author

xShivan commented Dec 29, 2021

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

@seangwright
Copy link
Owner

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
Copy link
Owner

Published as v1.0.1

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

Successfully merging a pull request may close this issue.

2 participants