5

I npm installed pixi.js into a typescript project. This line (found in many examples) does not work:

import * as PIXI from 'pixi.js'; 

I get the error "Cannot find module 'pixi.js'"

I tried this workaround and it works fine:

const PIXI = require('pixi.js');

The problem is that this loses the type information I want. Pixi has a d.ts file, so I should be getting types. What am I doing wrong here?

-e

Eric Jorgensen
  • 1,682
  • 2
  • 14
  • 23

3 Answers3

8

Make sure you use "moduleResolution": "node" in your tsconfig.json

Nihau
  • 474
  • 3
  • 20
0

It depends a lot on your entire configuration. If you only use typescript, without a bundler you can continue using the require option and in your tsconfig.json make sure you have :

include:[]

Pointing directly to your pixi... d.ts file.

If you use a bundler like webpack, it will depend on what version of PIXI you use.

Before 5.0.0, it was fairly simple and your first approach should do the trick. A while ago I created a demo repository for the same purpose: https://github.com/idzhurov/pixijs-game-boilerplate

In 5+ versions, everything is modular, so it is a bit more specific. You can try to start with the: import { Application } from 'pixi.js' and proceed from there.

Ivan Dzhurov
  • 187
  • 6
0

@Naxos84: If you can't use "moduleResolution": "node" try adding this to your tsconfig.json "compilerOptions"-object:

"paths": {
  "mini-signals": [
    "node_modules/resource-loader/typings/mini-signals.d.ts" 
  ],
   "resource-loader": [
     "node_modules/resource-loader/typings/resource-loader.d.ts"
   ],
   "pixi.js": [
     "node_modules/pixi.js/index.d.ts" 
   ],
   "*": ["*", "node_modules/*/index.d.ts", "node_modules/*/types/index.d.ts"]  
},
"allowSyntheticDefaultImports": true, 
"allowUmdGlobalAccess": true,         
"baseUrl": "./",
Martin Pabst
  • 861
  • 11
  • 9