【问题标题】:Cannot find module 'babylonjs'找不到模块'babylonjs'
【发布时间】:2020-01-02 05:06:52
【问题描述】:

index.ts

import { Engine } from '@babylonjs/core/Engine/engine';

[tsl] ERROR in ~/Documents/babylon1/src/index.ts(1,24)
  TS2307: Cannot find module '@babylonjs/core/Engines/engine'.

我已按照 Babylon 文档页面上的步骤以及https://doc.babylonjs.com/features/npm_support#error-ts2307-cannot-find-module-babylonjs-or-other-modules 的故障排除说明进行操作。我在 tscong.json 中添加了“babylonjs”,但我仍然收到“找不到模块 @babylonjs/core”错误。

package.json

"devDependencies": {
    "@babylonjs/core": "^4.0.3",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.4",
    "webpack": "^4.41.5",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.10.1"
},
"dependencies": {
  "babylonjs": "^4.0.3"
}

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist",
        "sourceMap": true,
        "noImplicitAny": true,
        "strictNullChecks": false,
        "module": "es6",
        "target": "es6",
        "types": [
            "babylonjs"
        ]
    }
}

我使用npm install 只是为了确保没有遗漏任何东西。

【问题讨论】:

  • 我通过 npm install -D @types/babylonjs 编译它,并将 import 更改为 babylonjs。这并不理想,因为它导入了整个 2.5MB 库,而不是单个模块,但它可以工作。

标签: babylonjs


【解决方案1】:

以下设置有效。 @babylonjs/core/... 导入正确解析,webpack-dev-server 自动构建和重新加载。

package.json

"devDependencies": {
  "@babylonjs/core": "^4.0.3",
  "ts-loader": "^6.2.1",
  "typescript": "^3.7.4",
  "webpack": "^4.41.5",
  "webpack-cli": "^3.3.10",
  "webpack-dev-server": "^3.10.1"
},
"dependencies": {}

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist",
    "module": "esNext",
    "target": "es6",
    "moduleResolution": "node"
  }
}

注意:types: ["babylonjs"] 似乎没有必要

webpack.config.js

module: {
  rules: [
    {
      test: /\.tsx?$/,
      use: 'ts-loader',
      exclude: /node_modules/
    },
  ],
},
resolve: {
  extensions: [ '.tsx', '.ts', '.js' ],
},
output: {
  filename: 'bundle.js'
},
devServer: {
  contentBase: path.join(__dirname, 'dist'),
  compress: true,
  hot: true
}

【讨论】:

    猜你喜欢
    • 2022-12-15
    • 2018-02-21
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多