"ReferenceError: _ is not defined" - how to fix?

After the upgrade my app won’t start throwing the following exception:

/XXX/.meteor/packages/meteor-tool/.1.7.0_1.8vm3nf.qhre5++os.osx.x86_64+web.browser+web.browser.legacy+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
						throw(ex);
						^

ReferenceError: _ is not defined
    at new Roles.Role (packages/nicolaslopezj:roles/roles.js:89:3)
    at roles.js (packages/nicolaslopezj:roles/roles.js:321:19)
    at fileEvaluate (packages/modules-runtime.js:339:7)
    at require (packages/modules-runtime.js:238:16)
    at /XXX/.meteor/local/build/programs/server/packages/nicolaslopezj_roles.js:859:1
    at /XXX/.meteor/local/build/programs/server/packages/nicolaslopezj_roles.js:869:3
    at infos.forEach.info (/XXX/.meteor/local/build/programs/server/boot.js:418:13)
    at Array.forEach (<anonymous>)
    at /XXX/.meteor/local/build/programs/server/boot.js:417:9
    at /XXX/.meteor/local/build/programs/server/boot.js:471:5
    at Function.run (/XXX/.meteor/local/build/programs/server/profile.js:510:12)
    at /XXX/.meteor/local/build/programs/server/boot.js:470:11

I suspect that this is due to this change in 1.7:

The underscore package has been removed as a dependency from meteor-base. This opens up the possibility of removing 14.4 kb from production bundles. Since this would be a breaking change for any apps that may have been using _ without having any packages that depend on underscore besides meteor-base, we have added an upgrader that will automatically add underscore to the .meteor/packages file of any project which lists meteor-base, but not underscore. Apps which do not require this package can safely remove it using meteor remove underscore.

See https://github.com/meteor/meteor/blob/devel/History.md#v17-2018-05-28 for all 1.7 changes.

You could try adding underscore to your app with meteor add underscore. However, as the nicolaslopezj:roles package fails to declare underscore as a dependency, I’m not sure whether it will work. Definitely worth a try, though!

1 Like

Thanks Rob,

Tried that but meteor replies underscore without a version constraint has already been added. And the error is still there.

Would there be a way to globally define _?

Well, yes - on the client you could coerce a global with window._ = _; and on the server with global._ = _;, as long as you’ve done import { _ } from 'meteor/underscore'; in each. However, that feels like a huge kludge and there’s no guarantee the global will have been set up in time for the package.

The only safe way to do this is to add underscore to the package dependencies in the package.js, which means cloning it. The need for ensuring that specific dependency has been in the Meteor API docs for as long as I can remember. Alternatively, if it’s actively maintained you could raise an issue on GitHub.

Could you expand a bit on this? How can I do that?

Here’s the link to The Meteor Guide which explains this:

https://guide.meteor.com/writing-atmosphere-packages.html#overriding-atmosphere-packages

Edit the package.js file and add 'underscore@1.0.10' to the list in api.use.

Note: that 1.0.10 is the latest Meteor version of underscore, but I don’t think it will have changed much.

1 Like

Thanks, I fixed it by copying the roles package into my local /packages dir and added an import statement.

2 Likes