首页 > 解决方案 > 编译器无法从 node_modules/@types 中找到类型

问题描述

我尝试P5从我的 node_modules 加载类型,但是当我编译我的代码时,终端返回此编译错误消息

Module not found: Error: Can't resolve 'p5' in '/Users/stan/En_cours/code/github/P5JS_TypeScript_WebPack/Template_V2/src'
 @ ./src/sketch.ts 2:0-25 23:18-20

在我使用的代码中导入,import * as p5 from "p5";但是当我阅读日志时,编译器尝试直接在src文件夹中搜索,我找不到要求进入node_modules/@types/p5.

有关信息,我是 TypeScript、NPM、Webpack 的初学者...

我也试过

import p5 from "../node_modules/@types/p5";
import * as p5 from "./node_modules/@types/p5";

我找到了这些链接,但没有找到可行的解决方案。

https://www.typescriptlang.org/docs/handbook/module-resolution.html

从已安装的@types 导入?

https://github.com/Microsoft/TypeScript/issues/16472

在我package.json

"devDependencies": {
    "@types/p5": "^0.9.0
    .../...
},

标签: node.jstypescripttypesp5.jsimport-module

解决方案


我修复了一部分问题。这是打字稿版本的问题,不知为什么我的版本package.json不是好版本,所以我用好的版本代替。另外还有一个导入p5问题,比如@ChristopherChiche 并且知道问题的大小export.js现在大约是 8MG 而之前是 10K,并且索引没有导出:(

我认为在导出过程P5中添加了,不确定但闻到了。如果是这样,如何不导出p5,因为我不需要,因为在 html 中它是这样写的<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>

链接项目: https ://github.com/StanLepunK/P5JS_typeScript_webPack/tree/master/Template_V2

export.js  8.49 MiB       0  [emitted]  [big]  export
Entrypoint export [big] = export.js
[1] (webpack)/buildin/global.js 472 bytes {0} [built]
[2] ./src/sketch.ts + 1 modules 1.27 KiB {0} [built]
    | ./src/sketch.ts 826 bytes [built]
    | ./src/Z_Position.ts 468 bytes [built]
    + 1 hidden module

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB).
This can impact web performance.
Assets:
  export.js (8.49 MiB)

WARNING in entrypoint size limit: The following entrypoint(s) combined asset size exceeds the recommended limit (244 KiB). This can impact web performance.
Entrypoints:
  export (8.49 MiB)
      export.js


WARNING in webpack performance recommendations:
You can limit the size of your bundles by using import() or require.ensure to lazy load some parts of your application.
For more info visit https://webpack.js.org/guides/code-splitting/

推荐阅读