Compile error: cannot require (Typescript)

Compile error: cannot require (Typescript)
0

#1

I’m using Cocos Creator 1.7

In my tsconfig.json i have

    "baseUrl": "./",
    "paths": {
      "mdolls/*" : ["assets/Script/*"]
    }

In my code i have:

import State from "mdolls/State";

In preview mode all is ok but i can’t buld the project:

Build Failed: Compile error: Cannot require 'mdolls/State' from '/Users/bishop/dev/mdolls/assets/Script'. Module not found.

#2

I’ve been trying to get this to work as well. It does work and bundles up fine if you run it in the preview. It doesn’t work in the actual Build process, there you will always get an error for it. I would like to find a solution for this as well!


#3

Is there any solution to this?


#4

cocos creator doesn’t use tsconfig imo.


#5

I checked actually its because basedir is only supported on typescript 2.0^. Cocos 1.7 is still using 1.x Typescript. Thats probably why…


#6

@ericmj did you find a solution for this?

I just tried in Cocos Creator 2.0.5 and preview works but after building it doesn’t understand it anymore. I would really like to find a solution for this. The code just gets that much cleaner :slightly_smiling_face:


#7

attach a demo, we take a look at this problem.


#8

@huanxinyin
I created a demo for you. It is very straight forward.

Set baseUrl and paths under compilerOptionsin tsconfig.json

Then import using your path name like this:

In my demo I have one cc.Label that says Default string by default, but has a component label-update.component.ts on it that will replace the cc.Label string into the value you get from the import in your component.


When you run the game in development mode you get as expected:
image

But problem comes from compiling the game (building it) and running it after the build it doesn’t work:
image
With errors from import:

The project I used to take these screenshot and prove my point is also attached into this post.

Thanks, hope you can fix it since it would clean up projects a lot.
typescript-path-compile-error.zip (954.7 KB)


#9

Did you have time @huanxinyin to try my demo? Please let me know if you have any additional questions about this :+1:


#10

Replace code and try:
import { labelString } from “import-me-using-paths”;


#11

@huanxinyin
The point is not to get it work with regular imports. Yes I can import it with
import { labelString } from “src/import-me-using-paths”;

But the point with this demo is to show how to use typescript Paths from tsconfig.json.

"baseUrl": "./",
    "paths": {
      "src/*" : ["assets/src/*"]
    }

and use it in an import. This is to structure your code better and have cleaner imports.

This works while running the game in development mode, but after build it doesn’t work anymore.

This would be very useful in bigger projects where you want a clean a nice code-base.

If you have any further questions on how to test it, feel free to ask :+1:


#12

How about this test demo? It maybe the point you want.

typescript-path-compile-error 2.zip (197.4 KB)


#13

Hi @huanxinyin thanks for your reply!

I tried the project but the problem is still there after you build the game. In development mode the game runs correct.

Remember before you build it you have to rename the project to not have any spaces in the path, otherwise you cannot build it (a creator bug).

Anyway after building it the problem happens:


#14

Eh… I get it! It is a bug, we create a issue task and discuss, it may be fixed on next version.


#15

@huanxinyin Great!

Yes, it would be so smooth to use these typescript Paths and since it works already in development I hope it’s not too big of a task to make it work after building the game.

Thank you!