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

TypeError: Cannot read property 'specifier' of undefined, instead of "A module cannot import itself" #1274

Closed
coni2k opened this issue Jan 18, 2017 · 4 comments

Comments

@coni2k
Copy link

coni2k commented Jan 18, 2017

When using "rollup" with a combination with a module that is trying to import itself (in my case "breeze-client"), it cannot reach to "A module cannot import itself" error message and fails with "TypeError: Cannot read property 'specifier' of undefined" error message.

It happens in the following block, at return declaration.source === source line:

if ( resolvedId === module.id ) {
	// need to find the actual import declaration, so we can provide
	// a useful error message. Bit hoop-jumpy but what can you do
	var name = Object.keys( module.imports )
		.find( function (name) {
			var declaration = module.imports[ name ];
			return declaration.source === source;
		});

	var declaration = module.imports[ name ].specifier.parent;

	module.error({
		code: 'CANNOT_IMPORT_SELF',
		message: "A module cannot import itself"
	}, declaration.start );
}

And the values of these variables:
declaration.source: commonjs-proxy:breeze-client
source: breeze-client

This leads to name variable becoming null (or undefined?) and then next line fails with "TypeError: Cannot read property 'specifier' of undefined" error message:

var declaration = module.imports[ name ].specifier.parent;

I have created a small demo app that shows the error. I tried to make the app as simple as possible, however I saw your "Issue Guideline" after preparing the application, and unfortunately it still contains typescript and gulp steps to show you the error.

I already spent some time to prepare this demo app but if you think it's still too much, I can even try to make it simpler.

https://github.com/forCrowd/Labs-RollupJS-InvalidAliasesFix

  • Download node packages
  • Compile typescript files
  • Run the the following gulp tasks to see each case:
  • build-invalidSettings-buggy: Uses invalid settings (no alias defined for "breeze-client". Rollup should show "A module cannot import itself" error message, but cannot due to "TypeError: Cannot read property 'specifier' of undefined" error.
  • build-invalidSettings-fixed: Again uses invalid settings, but experimentally fixed version of rollup and now rollup shows the correct error message.
  • build-valid: This tasks is here just to show that rollup works as expected with the correct settings.

Experimental fix that I did is simply changing this line:

return declaration.source === source;

to this:

return declaration.source.endsWith(source);

Also the actual error message can be improved by mentioning the module name like this?:

message: "A module cannot import itself: " + source

Probably this is not correct way of fixing this error but in case if you think it's fine for the moment, I can send you a pull request.

Stack trace TypeError: Cannot read property 'specifier' of undefined at ...\node_modules\rollup\dist\rollup.js:9557:47
@noamichael
Copy link

I had this exact same issue in my project - thank you ding finding this!

@ariporad
Copy link

Same here, this was causing me problems. Thanks so much!

ariporad added a commit to ariporad/rollup that referenced this issue Feb 24, 2017
@GordonSmith
Copy link

Me three..

@Rich-Harris
Copy link
Contributor

Fixed in 0.42 — thanks all

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

5 participants