3
votes

I've been struggling for a couple days now to get Typescript working in a simple template-generated Nativescript-Vue project. Here's what I've done:

  1. Created the project with them command:
vue init nativescript-vue/vue-cli-template typescript2
  1. Followed the instructions here: https://nativescript-vue.org/blog/using-typescript-in-nativescript-vue/

  2. Added an env.d.ts file with the contents:

declare var TNS_ENV: string;
  1. Added a shims.vue.d.ts file with the contents:
declare module '*.vue' {
    import Vue from 'vue'
    export default Vue
}
  1. Added the .ts extension to resolve extensions in webpack.config.js.
  2. Added the following rule in webpack.config.js.
{
    test: /\.ts$/,
    exclude: /node_modules|vue\/src/,
    loader: "ts-loader",
    options: {
        appendTsSuffixTo: [/\.vue$/]
    }
},
  1. Then running the app with the following command:
tns run android --bundle

This results in the following errors:

ERROR in Entry module not found: Error: Can't resolve './main.js' in '/Data/dev/test/Nativescript/typescript2/app'
Webpack compilation complete. Watching for file changes.
Webpack build done!
Executing before-shouldPrepare hook from /Data/dev/test/Nativescript/typescript2/hooks/before-shouldPrepare/nativescript-dev-webpack.js
Preparing project...
Executing before-prepareJSApp hook from /Data/dev/test/Nativescript/typescript2/hooks/before-prepareJSApp/nativescript-dev-webpack.js
Project successfully prepared (Android)
Executing after-prepare hook from /Data/dev/test/Nativescript/typescript2/hooks/after-prepare/nativescript-dev-webpack.js
Building project...
Gradle build...
         + setting applicationId
         + applying user-defined configuration from /Data/dev/test/Nativescript/typescript2/app/App_Resources/Android/app.gradle
Using support library version 28.0.0
         + adding nativescript runtime package dependency: nativescript-optimized-with-inspector
         + adding aar plugin dependency: /Data/dev/test/Nativescript/typescript2/node_modules/nativescript-socket.io/platforms/android/nativescript_socket.io.aar
         + adding aar plugin dependency: /Data/dev/test/Nativescript/typescript2/node_modules/tns-core-modules-widgets/platforms/android/widgets-release.aar
(node:3079) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
java.io.FileNotFoundException: /Data/dev/test/Nativescript/typescript2/platforms/android/build-tools/sbg-bindings.txt (No such file or directory)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileInputStream.<init>(FileInputStream.java:93)
        at org.nativescript.staticbindinggenerator.Generator.getRows(Generator.java:145)
        at org.nativescript.staticbindinggenerator.Generator.generateBindings(Generator.java:85)
        at org.nativescript.staticbindinggenerator.Generator.writeBindings(Generator.java:63)
        at org.nativescript.staticbindinggenerator.Main.main(Main.java:48)
Project successfully built.
Installing on device FA7A11A03046...
Successfully installed on device with identifier 'FA7A11A03046'.
Refreshing application on device FA7A11A03046...
System.err: java.lang.RuntimeException: Unable to create application com.tns.NativeScriptApplication: com.tns.NativeScriptException:
System.err:
System.err: Error calling module function
System.err:
System.err: Error: com.tns.NativeScriptException: Failed to find module: "./vendor", relative to: app/
System.err:     com.tns.Module.resolvePathHelper(Module.java:146)
System.err:     com.tns.Module.resolvePath(Module.java:55)
System.err:     com.tns.Runtime.runModule(Native Method)
System.err:     com.tns.Runtime.runModule(Runtime.java:553)
System.err:     com.tns.Runtime.run(Runtime.java:545)
System.err:     com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     android.app.ActivityThread.handleBindApplication(ActivityThread.java:5882)
System.err:     android.app.ActivityThread.access$1100(ActivityThread.java:200)
System.err:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1651)
System.err:     android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     android.os.Looper.loop(Looper.java:193)
System.err:     android.app.ActivityThread.main(ActivityThread.java:6680)
System.err:     java.lang.reflect.Method.invoke(Native Method)
System.err:     com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: File: "<unknown>, line: 1, column: 265
System.err:
System.err: StackTrace:
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.application/files/app/starter.js', line: 1, column: 61
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: Error: com.tns.NativeScriptException: Failed to find module: "./vendor", relative to: app/
System.err:     com.tns.Module.resolvePathHelper(Module.java:146)
System.err:     com.tns.Module.resolvePath(Module.java:55)
System.err:     com.tns.Runtime.runModule(Native Method)
System.err:     com.tns.Runtime.runModule(Runtime.java:553)
System.err:     com.tns.Runtime.run(Runtime.java:545)
System.err:     com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     android.app.ActivityThread.handleBindApplication(ActivityThread.java:5882)
System.err:     android.app.ActivityThread.access$1100(ActivityThread.java:200)
System.err:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1651)
System.err:     android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     android.os.Looper.loop(Looper.java:193)
System.err:     android.app.ActivityThread.main(ActivityThread.java:6680)
System.err:     java.lang.reflect.Method.invoke(Native Method)
System.err:     com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: File: "<unknown>, line: 1, column: 265
System.err:
System.err: StackTrace:
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.application/files/app/starter.js', line: 1, column: 61
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5887)
System.err:     at android.app.ActivityThread.access$1100(ActivityThread.java:200)
System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1651)
System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     at android.os.Looper.loop(Looper.java:193)
System.err:     at android.app.ActivityThread.main(ActivityThread.java:6680)
System.err:     at java.lang.reflect.Method.invoke(Native Method)
System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: Caused by: com.tns.NativeScriptException:
System.err:
System.err: Error calling module function
System.err:
System.err: Error: com.tns.NativeScriptException: Failed to find module: "./vendor", relative to: app/
System.err:     com.tns.Module.resolvePathHelper(Module.java:146)
System.err:     com.tns.Module.resolvePath(Module.java:55)
System.err:     com.tns.Runtime.runModule(Native Method)
System.err:     com.tns.Runtime.runModule(Runtime.java:553)
System.err:     com.tns.Runtime.run(Runtime.java:545)
System.err:     com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     android.app.ActivityThread.handleBindApplication(ActivityThread.java:5882)
System.err:     android.app.ActivityThread.access$1100(ActivityThread.java:200)
System.err:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1651)
System.err:     android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     android.os.Looper.loop(Looper.java:193)
System.err:     android.app.ActivityThread.main(ActivityThread.java:6680)
System.err:     java.lang.reflect.Method.invoke(Native Method)
System.err:     com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: File: "<unknown>, line: 1, column: 265
System.err:
System.err: StackTrace:
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.application/files/app/starter.js', line: 1, column: 61
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:
System.err: Error: com.tns.NativeScriptException: Failed to find module: "./vendor", relative to: app/
System.err:     com.tns.Module.resolvePathHelper(Module.java:146)
System.err:     com.tns.Module.resolvePath(Module.java:55)
System.err:     com.tns.Runtime.runModule(Native Method)
System.err:     com.tns.Runtime.runModule(Runtime.java:553)
System.err:     com.tns.Runtime.run(Runtime.java:545)
System.err:     com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     android.app.ActivityThread.handleBindApplication(ActivityThread.java:5882)
System.err:     android.app.ActivityThread.access$1100(ActivityThread.java:200)
System.err:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1651)
System.err:     android.os.Handler.dispatchMessage(Handler.java:106)
System.err:     android.os.Looper.loop(Looper.java:193)
System.err:     android.app.ActivityThread.main(ActivityThread.java:6680)
System.err:     java.lang.reflect.Method.invoke(Native Method)
System.err:     com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
System.err:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
System.err: File: "<unknown>, line: 1, column: 265
System.err:
System.err: StackTrace:
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:     Frame: function:'', file:'file:///data/data/org.nativescript.application/files/app/starter.js', line: 1, column: 61
System.err:     Frame: function:'require', file:'', line: 1, column: 266
System.err:
System.err:     at com.tns.Runtime.runModule(Native Method)
System.err:     at com.tns.Runtime.runModule(Runtime.java:553)
System.err:     at com.tns.Runtime.run(Runtime.java:545)
System.err:     at com.tns.NativeScriptApplication.onCreate(NativeScriptApplication.java:21)
System.err:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5882)
System.err:     ... 8 more
Successfully synced application org.nativescript.application on device FA7A11A03046.
^[[A^CExecuting after-watch hook from /Data/dev/test/Nativescript/typescript2/hooks/after-watch/nativescript-dev-webpack.js
Stopping webpack watch
Executing after-watch hook from /Data/dev/test/Nativescript/typescript2/hooks/after-watch/nativescript-dev-webpack.js
Stopping webpack watch

Is there anything I'm missing? I'd love to have the benefits of Typescript in a Nativescript-Vue project I'm about to start.

This is with the following versions:

Nativescript CLI: 5.0.3
Nativescript-Vue: 2.0.2
Webpack: 4.27.1

UPDATE: Quick note, when creating the app in step 1, I chose Simple template, installed VueX, and installed Vue-Devtools.

3
are you able to run the project without adding typescript? I have created a new project using vue init nativescript-vue/vue-cli-template typescript2 and run the tns run android --bundle and it's giving me same error.Narendra
@NarendraMongiya Yes, I got it running no problem without Typescript. Maybe check the versions of the CLI, Nativescript-Vue, and Webpack you're using?Jason

3 Answers

3
votes

Update (30-Jan-2019): Good news - I think they are adding out-of-the-box support for Typescript with the template in this PR.

So from now on we should be able to choose Typescript as the language option when bootstrapping the project with the template.


I had encountered the same problem and here is what I did to add TypeScript to my Nativescript-Vue project. (I do not have enough reputation to add a comment so I have to post this as an answer)

  1. Create the project with vue init nativescript-vue/vue-cli-template <project-name>
    • There is a warning about webpack.config.js during the project creation and I was asked to run ./node_modules/.bin/update-ns-webpack --configs
    • Vue Devtools does not work with tns preview so I had disabled it as well
  2. Add the following:
    • Install typescript and vue as devDependencies
    • Rename app/main.js to app/main.ts
    • In app/main.ts, change import App from './components/App' to import App from './components/App.vue'
    • Add tsconfig.json (copied from this stater project AND commented out "strict": true,)
    • In webpack.config.js, add 'vue$': 'nativescript-vue' under alias and exclude: /node_modules|vue\/src/, under module.rules (to the one for ts - details here)
    • As you has mentioned in your step 3 and 4, I had also created the env.d.ts and shims.vue.d.ts under types/ folder accordingly (Note that in the tsconfig.json file I copied, it had defined "typeRoots": ["types"])
  3. I had tested it with tns preview --bundle
    • The app is running fine on my android phone (no errors, changes can be seen on the phone)
    • if necessary, I can test that if it would work with the command tns run android --bundle

Hope the above can help.

Version

Versions used are as follow:

  • Nativescript CLI: 5.1.0
  • nativescript-vue: 2.0.2
  • webpack: 4.28.3

Reference

  1. This is the git commit I did in order to add TypeScript to the project.
  2. The official blog post about adding TypeScript to nativescript-vue - It was very helpful but unfortunately it did not work immediately after following those steps (maybe because I had used a different vue-init template)
  3. And of course kudos to the detailed steps listed in this stack overflow question which had given me the direction to fix my project :-)
3
votes

There is also now a Vue CLI 3 plugin for Nativescript-Vue that will scaffold out an entire project in Javascript or Typescript and then utilize the Vue CLI 3 infrastructure to help you. Disclaimer: I'm the primary contributor to the current state of the plugin.

It also includes the ability to have code sharing between web and native projects or you can proceed with just a native only project.

You can utilize the CLI 3 UI experience or use the terminal. Either way, you can do the following:

  1. Create a new Vue project and make sure you choose Typescript. In the terminal this would be vue create my-app and then in the prompts, choose Typescript.
  2. If you are in the terminal, make sure you change to the project's directory. If you are in the UI, then this is unnecessary.
  3. Add the plugin to the project. In the terminal this would be vue add vue-cli-plugin-nativescript-vue.
0
votes

If you like templates with out of the box support for nativescript navigator (router), vuex, typescript/eslint, prettier, and structured using module-oriented approach (https://mnapoli.fr/organizing-code-into-domain-modules/) for easy maintenance as your app grows, you may clone our open-source project: https://github.com/Lyduz/nitibo