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

Including files from node_modules in index.html causes export issues. #1483

Closed
dumptyd opened this issue Jun 3, 2018 · 4 comments
Closed
Labels

Comments

@dumptyd
Copy link

dumptyd commented Jun 3, 2018

🐛 bug report

In my index.html if I do

<script src="./node_modules/some_package/some_file_that_exposes_myModule.js">
<script>
  myModule();
</script>

It compiles fine but in the browser I get myModule is not defined.

🤔 Expected Behavior

Included file should expose the instance/object correctly.

Software Version(s)
Parcel 1.7.1
Node 8.0.0
Yarn 0.21.3
Operating System Ubuntu Xenial
@KeineLimonade
Copy link

KeineLimonade commented Jun 4, 2018

Does your file in node_modules expose myModule as a global variable? Most modules don't do that.
To solve this you could put the calling script in an extra file and require the module there.

@dumptyd
Copy link
Author

dumptyd commented Jun 6, 2018

That works in some cases, but I can't even get something as simple as this to work.

require('jquery/dist/jquery.js');
require('bootstrap/dist/js/bootstrap.min.js');

because bootstrap can't find jquery.

@mischnic
Copy link
Member

mischnic commented Jan 6, 2019

The issue with jQuery is that it detects that module.exports is available, so it doesn't set window.jQuery:
node_modules/jquery/dist/jquery.js:

	if ( typeof module === "object" && typeof module.exports === "object" ) {

		// For CommonJS and CommonJS-like environments where a proper `window`
		// is present, execute the factory and get jQuery.
		// For environments that do not have a `window` with a `document`
		// (such as Node.js), expose a factory as module.exports.
		// This accentuates the need for the creation of a real `window`.
		// e.g. var jQuery = require("jquery")(window);
		// See ticket #14549 for more info.
		module.exports = global.document ?
			factory( global, true )/* runs in node & parcel*/ :
			function( w ) {
				if ( !w.document ) {
					throw new Error( "jQuery requires a window with a document" );
				}
				return factory( w );
			};
	} else {
		factory( global ); /* runs in browser */
	}

@mischnic
Copy link
Member

mischnic commented Jan 7, 2019

Duplicate of #333

@mischnic mischnic marked this as a duplicate of #333 Jan 7, 2019
@mischnic mischnic closed this as completed Jan 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants