Error - includes is not a function inside .es6

Enonic version: 7.12.1
OS: Linux

[1,2,3,4,5,6,7,8,9].includes(5);

There is a problem with includes function inside .es6 file:

Caused by: jdk.nashorn.internal.runtime.ECMAException: TypeError: [1, 2, 3, 4, 5, 6, 7, 8, 9].includes is not a function
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.Undefined.lookup(Undefined.java:100)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:106)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:96)
	at jdk.dynalink/jdk.dynalink.linker.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:161)
	at jdk.dynalink/jdk.dynalink.linker.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:109)
	at jdk.dynalink/jdk.dynalink.LinkerServicesImpl.lambda$getGuardedInvocation$0(LinkerServicesImpl.java:137)
	at jdk.dynalink/jdk.dynalink.LinkerServicesImpl.getWithLookupInternal(LinkerServicesImpl.java:168)
	at jdk.dynalink/jdk.dynalink.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:135)
	at jdk.dynalink/jdk.dynalink.DynamicLinker.relink(DynamicLinker.java:242)
	at jdk.scripting.nashorn.scripts/jdk.nashorn.internal.scripts.Script$Recompilation$640$423A$user.L:1#post(isnareactcms:/services/user/user.js:32)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:657)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:513)
	at jdk.scripting.nashorn/jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:527)
	at jdk.scripting.nashorn/jdk.nashorn.api.scripting.ScriptObjectMirror.call(ScriptObjectMirror.java:120)
	at com.enonic.xp.script.impl.value.FunctionScriptValue.call(FunctionScriptValue.java:38)

Hi Nikolay,

XP7 is running on the Nashorn-platform, and we are largely limited to ES5-libraries from the platform.

I usually just work around your issue with this:

if ([1,2,3,4,5,6,7,8,9].indexOf(5) !== -1) {
  ...
}

I usually don’t bother with it, but you can polyfill the functions you need if you are using the webpack-starter.

You need to add the polyfill here: starter-webpack/src/main/resources/polyfills.js at master · enonic/starter-webpack · GitHub

And update the webpack.server.config: starter-webpack/webpack.server.config.js at master · enonic/starter-webpack · GitHub

:warning: Webpack will add the polyfill to the beginning of every JS-file, so this might give you longer build times as well as a larger bundle size.

I limit my use of it to when npm-libraries I depend on needs the polyfill to work.

– Tom Arild

2 Likes