Hi there,
I’m new here and this is my first posting.
My home is web development and I was happy when I found Capacitor as it makes it possible to make Android and iOS Apps based on HTML, CSS and Javascript. So I went ahead and started to create my first app.
One of the first problems I encountered is that Capacitor uses import syntax for including plugins which is not available in client side javascript. I searched for a workaround and found browserify.js
Is working so far after I understood it but I would prefer a better solution. Use Ionic? Or develop in node.js?
Currently I need to have access to the file system. Found the file system API and I’m unsure if I should stick to browserify or use an alternative.
Any hints are welcome, thanks in advance.
Best regards, Ulrich
if you setup your development environment for ionic correctly you can use import syntax. I recommend visual studio code with the ionic extension, but you can also use the terminal API of ionic to build the app. Then ionic will do all imports and build an app bundle.
Installed extension in VS Code - no more errors are indicated when using the import statement - fine
Installed Ionic in my app folder by use of npm - worked
However, when running the app in Android Studio and view the console by use of the inspect feature of chrome, still the following error is reported:
“SyntaxError: Cannot use import statement outside a module”
What am I missing now?
Best regards, Ulrich
PS: My javascript:
<body>
Hallo Welt!
<button>Fill Database</button>
<section class="substances"></section>
<!-- <script src="bundle.js"></script> -->
<script>
import { CapacitorSQLite } from '@capacitor-community/sqlite';
import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
// custom javascript here
Edit: I forgot something important: I have to include Ionic itself by these lines:
I wasn’t the developer that doesn’t init the project folder in our app, so I’m not a perfect help.
Unfortunately the response time in this forum is very high.
I think our folder structure is a standard react structure, so I’m not sure with your architecture.
Hi Jannis,
many thanks for your support, I appreciate it.
Apart from Ionic, which I added today, I do not use any framework.
bundle.js was created by browserify.js. I used it as a workaround as it was not possible to use import syntax. After switching to Ionic I assumed it’s no longer necessary and therefore I commented it out.
Just by adding Ionic libraries to your project that doesn’t make it be an Ionic project with all the Ionic apps benefits.
When you create a new Ionic project using Ionic CLI, it creates a project that uses typescript, has a bundler configured such as vite or webpack depending on the framework you’ve chosen, etc.
By adding the Ionic libraries to an existing project you don’t get any of those.
If you want to use the import syntax you need a bundler, I think vite is the simpler and all major frameworks seems to be moving to using it.
You also need to specify that the javascript is a “module”, so if you use it in index.html you have to add this to your script tag:
<script type="module">
/* JavaScript module code here */
</script>
Or if you want to use it in a separate javascript file, then it should look like <script type="module" src="myimportcode.js"></script>
If you don’t want to use the import syntax and you don’t target web, you can still use plugins like this.
Capacitor.Plugins.PluginName.pluginMethod()
real example Capacitor.Plugins.Device.getInfo().
But you still need a bundler that copies the plugin files from node_modules to your app.