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

Main module not found in 3.0.0 (works in 2.1.0) #121

Open
vytenisu opened this issue Feb 15, 2019 · 6 comments · May be fixed by #122
Open

Main module not found in 3.0.0 (works in 2.1.0) #121

vytenisu opened this issue Feb 15, 2019 · 6 comments · May be fixed by #122

Comments

@vytenisu
Copy link

vytenisu commented Feb 15, 2019

Hello there!

First of all - thank you for the amazing package!

I have noticed that dts-generator stopped working for me in the new (3.0.0) version. I am running it on Windows.

Please see my experiment below:

  1. Installed 3.0.0
  2. Got "module not found" error
  3. Installed 2.1.0
  4. Worked fine with same command

Command line:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS F:\Test\www\ter> npm install [email protected]
npm WARN [email protected] requires a peer of acorn@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ [email protected]
removed 2 packages, updated 1 package and audited 24088 packages in 12.008s
found 0 vulnerabilities

PS F:\Test\www\ter> npm run dts

> [email protected] dts F:\Test\www\ter
> dts-generator --name ter --project ./ --out index.d.ts --main ter/index

Starting
processing:
  F:/Test/www/ter/lib/interfaces.ts
  F:/Test/www/ter/lib/config.ts
  F:/Test/www/ter/lib/sources/av.ts
  F:/Test/www/ter/lib/sources/magic.ts
  F:/Test/www/ter/lib/sources/mc.ts
  F:/Test/www/ter/lib/er.ts
  F:/Test/www/ter/index.ts
  F:/Test/www/ter/lib/test-utils.ts
(node:3064) UnhandledPromiseRejectionWarning: Error: main module ter/index was not found
    at F:\Test\www\ter\node_modules\dts-generator\index.js:295:23
    at new Promise (<anonymous>)
    at Object.generate [as default] (F:\Test\www\ter\node_modules\dts-generator\index.js:232:16)
    at Object.main [as default] (F:\Test\www\ter\node_modules\dts-generator\bin\dts-generator.js:70:31)
    at Object.<anonymous> (F:\Test\www\ter\node_modules\dts-generator\bin\dts-generator:5:22)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
(node:3064) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:3064) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
PS F:\Test\www\ter> npm install [email protected]
npm WARN [email protected] requires a peer of acorn@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ [email protected]
added 2 packages from 2 contributors, updated 1 package and audited 24088 packages in 11.757s
found 0 vulnerabilities

PS F:\Test\www\ter> npm run dts

> [email protected] dts F:\Test\www\ter
> dts-generator --name ter --project ./ --out index.d.ts --main ter/index

Starting
processing:
  F:/Test/www/ter/lib/interfaces.ts
  F:/Test/www/ter/lib/config.ts
  F:/Test/www/ter/lib/sources/av.ts
  F:/Test/www/ter/lib/sources/magic.ts
  F:/Test/www/ter/lib/sources/mc.ts
  F:/Test/www/ter/lib/er.ts
  F:/Test/www/ter/index.ts
  F:/Test/www/ter/lib/test-utils.ts
Aliased main module ter to ter/index
output to "index.d.ts"
Done!
@dylans
Copy link
Contributor

dylans commented Feb 18, 2019

Hi @vytenisu, thanks for the report, I'm not seeing anything obvious.

Do you have any idea what is causing this line in your 2.1.0 version?

Aliased main module ter to ter/index

I'm wondering if maybe you had set something up to get this working earlier. Or perhaps there was a feature in earlier versions that we did not realize existed.

@vytenisu
Copy link
Author

vytenisu commented Feb 19, 2019

Did some testing on my project and remembered why I was using "--main" together with "--name" and also what "alias" is.

Use case:

  1. Export (or re-export) functionality from main npm module file.
  2. Bundle sources into single JS file
  3. Generate index.d.ts
  4. Npm ignore all source files to reduce NPM package size leaving only the bundle JS, d.ts and package.json
  5. Link this bundled module and use from another TS library.

What happens:

2.1.0 (no --main and no --name)

index.d.ts ends like this:

declare module 'some-library/index' {
	export * from 'some-library/lib/some-class';
}

If you try to import such library, module will not be found based on TS.

2.1.0 (with --main and with --name)

index.d.ts ends like this (alias is created):

declare module 'some-library/index' {
	export * from 'some-library/lib/some-class';
}
declare module 'some-library' {
	import main = require('some-library/index');
	export = main;
}

Bundled library can successfully be imported.

3.0.0 (no --main and no --name)

index.d.ts ends like this:

declare module 'index' {
	export * from 'lib/logger';
}

It does not work when importing library. Also "index" is not prefixed with library name because in 3.0.0 --name has to be used together with --main. In older versions --name was mandatory and could be used without --main.

3.0.0 (with --main and with --name)

Error :)

main module some-library/index was not found

So basically this "alias" is necessary for my use case. I have not looked into dts-generator code yet to understand the issue but above description should help understand the use case at least.

@vytenisu
Copy link
Author

vytenisu commented Feb 19, 2019

Found the problem. Turns out I needed to also use:

 --prefix some-library

Without this option prefix is converted to string as "undefined" here:

if (options.main && options.main === (options.prefix + filenameToMid(sourceFile.fileName.slice(baseDir.length, -3)))) {
    foundMain = true;

Which is a bug because without prefix "main" will never match under normal conditions.

Maybe it is just me but even after fixing prefix locally I still could not generate a working .d.ts unless I provided module name as a prefix. Got confused a bit tbh :)

This comment I found in source code explains code change history a bit:

// since options.name used to do double duty as the prefix, let's be
// considerate and point out that name should be replaced with prefix.
// TODO update this error message when we finalize which version this change
// will be released in.

@vytenisu
Copy link
Author

vytenisu commented Feb 27, 2019

Provided comment in PR by @msssk . Thank you!

In the meantime I did something silly and after playing around a bit ended up publishing "npm-dts" and "npm-dts-webpack-plugin" packages. I wanted to have minimal configuration and simplicity for my use case. Should those packages be of any use for you or development of "dts-generator"- check them out.

@lemon-clown
Copy link

lemon-clown commented Jun 6, 2019

I found a simple method to make it works (as a temporary solution).

  1. set the value of prefix to ''
  2. set the value of main with a prefix /

for expample:

 {
   prefix: '',
   name: 'demo',
   main: '/index',  // while the index.ts is your entry file.
 }

I hope I made myself clear :)

@schdck
Copy link

schdck commented Jan 12, 2021

For some reason, the mapping of name -> main only exports the default export. So if you followed the steps above and you are getting an empty module, like this:

declare module 'src/index' {
	export class MyClass {}

	export {};

}
declare module '@my-org/my-lib' {
	// Missing export of MyClass here
}

Make sure you have a default export on your main file. This way, the module will have the default export in it:

declare module 'src/index' {
	export class MyClass {}

	export default MyClass;

}
declare module '@my-org/my-lib' {
	export {default} from '/src/index';
}

Also, keep in mind that if you're using @lemon-clown's temporary solution, you'll have to fix the / before src on export {default} from '/src/index';

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.

4 participants