优先注意事项:Stackoverflow上存在大量“找不到相对模块”问题。我已经浏览了尽可能多的内容,它们不是重复的。请不要将此标记为重复。
我有一个vue create
设置的使用Babel和Typescript的Vue项目,然后我使用以下命令生成了一些gRPC-Web / Protobuf代码:
cd src
protoc -I=../../protos ../../protos/foo.proto --js_out=import_style=typescript:. --grpc-web_out=import_style=typescript,mode=grpcweb:.
这会在src
目录中生成以下文件:
foo_pb.d.ts
(Protobuf消息的Typescript定义)empty.js
,memory.js
(Protobuf消息)FooServiceClientPb.ts
(gRPC网络代码)但是,当我npm run serve
或npm run build
出现此错误时:
This relative module was not found:
* ./foo_pb in ./src/main.ts, ./src/FooServiceClientPb.ts
在main.ts
和FooServiceClientPb.ts
中都这样做:
import {Empty, Memory} from './foo_pb';
我可以按住Ctrl键单击./foo_pb
,它会正确打开foo_pb.d.ts
,因此VSCode至少知道如何打开该文件。而且所有文件都在同一目录中!为什么构建系统找不到它?
我尝试了标准删除node_modules
,请了律师,上了体育馆。也是npm-check-updates -u
。什么都没有。
通过在产生错误的位添加console.log()
,我又走了一点(显然,Vue不会输出实际错误,是的)。我知道了:
resolve './foo_pb' in '/home/me/myproject/src'
using description file: /home/me/myproject/package.json (relative path: ./src)
Field 'browser' doesn't contain a valid alias configuration
using description file: /home/me/myproject/package.json (relative path: ./src/foo_pb)
no extension
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.js doesn't exist
.jsx
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.jsx doesn't exist
.vue
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.vue doesn't exist
.json
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.json doesn't exist
.wasm
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.wasm doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
/home/me/myproject/src/foo_pb.tsx doesn't exist
as directory
/home/me/myproject/src/foo_pb doesn't exist
如果您运行vue ui
并签出Inspect
任务(已解析的Webpack配置),则会看到:
resolve: {
extensions: [
'.mjs',
'.js',
'.jsx',
'.vue',
'.json',
'.wasm',
'.ts',
'.tsx'
],
...
请注意,它缺少.d.ts
。因此,我尝试通过创建具有以下内容的文件vue.config.js
来添加它:
module.exports = {
configureWebpack: {
resolve: {
extensions: [
".d.ts"
]
}
}
}
但是这也不起作用:
ERROR Failed to compile with 1 errors 12:36:53
error in ./src/foo_pb.d.ts
Module build failed (from ./node_modules/thread-loader/dist/cjs.js):
Thread Loader (Worker 0)
Debug Failure. Output generation failed
at PoolWorker.fromErrorObj (/home/me/myproject/node_modules/thread-loader/dist/WorkerPool.js:262:12)
at /home/me/myproject/node_modules/thread-loader/dist/WorkerPool.js:204:29
at Object.transpileModule (/home/me/myproject/node_modules/typescript/lib/typescript.js:112341:29)
at getTranspilationEmit (/home/me/myproject/node_modules/ts-loader/dist/index.js:283:74)
at successLoader (/home/me/myproject/node_modules/ts-loader/dist/index.js:66:15)
at Object.loader (/home/me/myproject/node_modules/ts-loader/dist/index.js:22:12)
答案 0 :(得分:0)
好的,我认为,这只是protoc
中的错误。我是这样运行的:
protoc -I=../../protos/ ../../protos/foo.proto --js_out=import_style=typescript:. --grpc-web_out=import_style=typescript,mode=grpcweb:.
这会创建foo_pb.d.ts
,但它也应该 创建foo_pb.js
。实际上,它为每个消息-memory.js
和empty.js
创建了单独的文件。我通过运行以下命令“修复”了它:
protoc -I=../../protos/ ../../protos/foo.proto --js_out=import_style=commonjs,binary:. --grpc-web_out=import_style=typescript,mode=grpcweb:.
然后生成了foo_pb.js
文件,并且它还生成了foo_pb.d.ts
文件。我知道吗,WTF?