简体   繁体   English

tsconfig 路径和 webpack 别名 - 找不到模块问题

[英]tsconfig path and webpack alias - cannot find module issue


myCustom and ToDo are each react app and want to import MyCustom as a module inside ToDo app. myCustom 和 ToDo 都是 react 应用程序,并且希望将 MyCustom 作为 ToDo 应用程序中的模块导入。
so following was my trial / folder structure:所以以下是我的试用/文件夹结构:

[api]
|
[client]
|- [MyCustom]
|- [ToDo]
|  |- [src] (in src, will use - import {} from 'myCustom')
|  |- tsconfig.json (added "paths": { "myCustom": ["../myCustom/src"] })
|  |- package.json (added dependency - "myCustom": "1.0.0")

and in ToDo app, when I try npm install , it says myCustom is not in the npm registry.在 ToDo 应用程序中,当我尝试npm install时,它说myCustom不在 npm 注册表中。
can you point out what's wrong here, please?你能指出这里有什么问题吗?

_____________________________ _____________________________

on TODO webpack.config.js file, I've added在 TODO webpack.config.js 文件上,我添加了

resolve: {
   modules: ['../MyCustom/src']
},
resolveLoader: {
   modules: ['../MyCustom/src', 'myCustom']
}

and built failed with following error:并构建失败并出现以下错误:
Module not found: Error: Can't resolve 'source-map-loader'.找不到模块:错误:无法解析“source-map-loader”。


Am I using a loader wrong?我是否使用了错误的装载机?

resolve is a main topic in webpack.解决是 webpack 中的一个主要主题。

  1. please read how to resolve alias and don't work with '../' in webpack请阅读如何解决别名并且不要使用 webpack 中的 '../'
  2. then define where modules can be found然后定义可以找到模块的位置
  3. define where loaders can be found定义可以找到加载器的位置
  
    // https://github.com/unic/darvin-webpack-boilerplate/blob/master/webpack.config.js
    const webpackConfigProd ={
        resolve: {
          modules: ['node_modules', '@yourAlias/yourCustomDir']
        },
        resolveLoader: {
          modules: ['node_modules', '@yourAlias/loaderDir']
        }
    };
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM