我正在為客戶端 javascript 代碼制作一個打字稿專案。
在我使用 typescript 之前,我匯入了一個這樣的模塊(這是 es6 中的 vanilla js)
import * as THREE from 'https://threejs.org/build/three.module.js';
但是使用打字稿,我安裝了模塊,npm install然后像這樣匯入它
import * as THREE from 'three';
問題是,當我運行編譯的 js 代碼時,我得到這個瀏覽器 js 錯誤:
Uncaught TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".
因為import * as THREE from 'three';現在在編譯的 js 檔案中。
我怎樣才能解決這個問題?
謝謝

tsconfig.json
{
"compilerOptions": {
"outDir": "dist/",
"rootDir": "./src",
"sourceMap": false,
"noImplicitAny": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"module": "ESNext",
"target": "ESNext",
"lib": [
"ESNext", "DOM"
],
"allowJs": false,
"alwaysStrict": true,
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"src/",
"src/**/*.json"
],
"exclude": [
]
}
uj5u.com熱心網友回復:
為了在客戶端使用它,需要一個捆綁器。
我建議使用 webpack 或 snowpack。Snowpack 是新的并且有很多非常酷的功能。
uj5u.com熱心網友回復:
使用 NPM 安裝的包通常位于該node_modules檔案夾中。這意味著您可以通過它的相對路徑訪問該庫:
import * as THREE from './node_modules/tree/build/three.module.js';
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411558.html
標籤:
下一篇:擴展已定義方法的引數
