Node Modules in Cocos Bundles


I’m trying to get Node Modules to work in a Bundle, but without success. As long as the TypeScript files are not in a bundle, it works fine but within a bundle it doesn’t work.

  1. I can import a “non-bundle” (main) script file into a “bundle script file” (e.g. “test” bundle) without creating a global variable (which is nice!) even though documentation says:

It is recommended that scripts in the different Asset Bundles do not reference each other, otherwise you may not find the corresponding script at runtime. If you need to reference certain classes or variables, you can share them by exposing them in your own global namespace.

  1. I can NOT reference a node_module path from a bundle, but I can from a “non-bundle” (main).
  2. I can create a file in the “non-bundle”(main) forwarding the variable of a node_module library, like this:
export const rxjs = import('rxjs');

and using it like this, from the bundle file

const rx = await rxjs;

But this is a very ugly workaround.

How can I just import and reference node_modules without re-assigning a variable for it?
I don’t want the node_module libraries to be cloned into both bundles. I only want it to be in the “non-bundle”(main bundle) or maybe even better in a separate module which loads before main or any other module.

Most important is that I would always be able to just import it straight from node_modules and not having to redirect it into some weird variables depending on from where I load it.

@pandamicro You probably know the solution I’m looking for here :slight_smile:

Cocos Creator version: 2.4.6.

Let me ask engineering, I know @pandamicro has been busy.

Hi, npm packages are not recommended to be put into bundle for use. You can try using webpacker to package it as a js script to use?

Thanks for the feedback. We have discussed the introduction of rxjs internally and will give a better solution as soon as possible.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.