【发布时间】:2022-01-16 00:11:09
【问题描述】:
我正在为客户端 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": [
]
}
【问题讨论】:
-
您的项目结构是什么样的?你有 package.json 吗?一个 tsconfig.json?
-
你是怎么编译的?很有可能,您需要编译 AND 捆绑,以便您的捆绑器将 NPM 依赖项捆绑到已编译的代码中。
-
我已经添加了我的项目结构和tsconfig。
-
你能包含你的 package.json 吗?您还应该尝试重新启动 ts 服务器 stackoverflow.com/questions/64454845/…
标签: javascript node.js typescript npm