Skip to content

Nest.js: Unable to import #27

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
theheckwr opened this issue Dec 27, 2021 · 12 comments
Closed

Nest.js: Unable to import #27

theheckwr opened this issue Dec 27, 2021 · 12 comments

Comments

@theheckwr
Copy link

Problem

Compilation fails when using tle.js with nest.js:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: <redacted>/node_modules/tle.js/dist/tlejs.umd.js
require() of ES modules is not supported.
require() of <redacted>/node_modules/tle.js/dist/tlejs.umd.js from <redacted>/dist/services/spacecraft-data.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 tlejs.umd.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from <redacted>/node_modules/tle.js/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (<redacted>/src/services/spacecraft-data.service.ts:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)

Workaround

Remove the "type": "module", line from the package.json

Solution

I don't know (yet). Maybe remove the line and everything would be fine? Or does that cause other problems?

@davidcalhoun
Copy link
Owner

davidcalhoun commented Dec 27, 2021

Thanks for the heads up, it looks like Nest.js doesn't yet support ES Modules: nestjs/nest#8736 (also see nestjs/nest#7021)

The good news is that it's a quick fix for you - you can tell it to explicitly use the CommonJS dist instead:

const { getLatLngObj } = require("tle.js/dist/tlejs.cjs");

I verified this works by running their TypeScript starter project and outputting some test code in their app.service.ts Hello World (note that I'm using Node 16.13.1):

import { Injectable } from '@nestjs/common';
const { getLatLngObj } = require("tle.js/dist/tlejs.cjs");

const tle = `ISS (ZARYA)
1 25544U 98067A   17206.18396726  .00001961  00000-0  36771-4 0  9993
2 25544  51.6400 208.9163 0006317  69.9862  25.2906 15.54225995 67660`;

@Injectable()
export class AppService {
  getHello(): string {
    return `Hello World! ${JSON.stringify(getLatLngObj(tle))}`;
  }
}

@theheckwr
Copy link
Author

Okay, thanks for the info and the "temporary" solution.
That's way better than my workaround. I've implemented it and it works so far.
Let's hope they'll implement it soon.

@davidcalhoun
Copy link
Owner

davidcalhoun commented Dec 27, 2021

Great, glad that works! I am seeing a similar error with a simple Node project so I think I may be packaging it a bit wrong as well, looking into this!

@davidcalhoun
Copy link
Owner

In tle.js 4.5.0 I've added Node.js conditional exports support which seems to resolve the issue!

@theheckwr
Copy link
Author

theheckwr commented Dec 27, 2021

Unfortunately when I try to import this version with TypeScript into my Nest.js project, that fails with a "Can't find module declaration or corresponding type declaration" (Even my IDE (vs-code) shows this...).
Maybe this is a problem again with Nest.js but it looks like a TypeScript thing to me. Maybe something with my TypeScript config?

Additionally the other way you described before now gives an error:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/tlejs.cjs' is not defined by "exports" in <redacted>/node_modules/tle.js/package.json
    at throwExportsNotFound (internal/modules/esm/resolve.js:299:9)
    at packageExportsResolve (internal/modules/esm/resolve.js:522:3)
    at resolveExports (internal/modules/cjs/loader.js:449:36)
    at Function.Module._findPath (internal/modules/cjs/loader.js:489:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:875:27)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (<redacted>/src/services/spacecraft-data.service.ts:5:84)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)

@davidcalhoun
Copy link
Owner

Oof, thanks for the heads up, I'm not sure what's going on there. As a workaround you'll have to lock it down to the prior version for now (npm i [email protected]). Sorry for the hassle!

@theheckwr
Copy link
Author

Yes, that's what I did already.
But thanks anyways for the help so far.

@davidcalhoun
Copy link
Owner

When you have a chance, please try again with 4.5.3, I believe I finally tracked down the issue! Thanks again.

@theheckwr
Copy link
Author

Okay, I tested it.
It works so far. No errors and the functions I'm using are executing normally, as far as I see. Even with Nest.js.
Thanks very much!

@davidcalhoun
Copy link
Owner

Woo! Good stuff, thanks for your patience!

@thkruz
Copy link
Contributor

thkruz commented Jul 26, 2022

@davidcalhoun can you close this?

@davidcalhoun
Copy link
Owner

Sounds good, closing this out!

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

3 participants