'Failed to find module' error when attempting to resolve relative modules outside of NativeScript root directory


If I attempt to import a module from a directory outside of the app directory in an NativeScript (2.5.1) Angular application, when executing tns run android, I get an error in Android console of the form:

Error: com.tns.NativeScriptException: Failed to find module: "../../path-to-module" relative to: app/com.tnts.Module.resolvePathHelper(Module.java:159)

TypeScript (2.2.2) has no problem finding the module, as there are no transpilation errors during the build.

Is there a way to configure the NativeScript CLI to bundle modules located outside the project app directory?


Never tried that personally. You could dump it in node_modules, of course that’s not good for source control or nuking your node_modules :slight_smile: - any reason you don’t want to store it in the app directory?

If you want to extend the CLI: https://github.com/NativeScript/nativescript-cli/blob/master/extending-cli.md that should help.


The app folder is what gets bundled as assets on your device.

The node_modules production dependencies go into app/tns_modules on the device.

In addition to the runtimes following the CommonJS specification for modules, they add an additional rule - do not search for scripts beyond the app.

So there is no reason you should need to import scripts outside of the app working directory.


Thanks for the informative replies. The reason for storing the modules in question outside of the NativeScript app directory is that they contain “isomorphic” code shared between the client and server in separate project directories in the same solution repository, so I was attempting to maintain a single DRY copy in a logical location.

For now, I’ve accomplished that by using creating a symlink to the shared directory inside the NativeScript app directory, but am still running into the issue of NativeScript’s build process transpiling to JavaScript in the same directory instead of a separate outDir, which the server build tool doesn’t appreciate. If that behavior can’t be changed, I think I’ll have to extend the CLI the way @bradwaynemartin mentions by adding a hook to copy the shared files into a directory inside app.

Recommendations for a simple file copy module appreciated!