简体   繁体   English

LernaJS Typescript 找不到模块

[英]LernaJS Typescript cannot find module

I'm trying to use LernaJS with typescript and I have some problems.我正在尝试将 LernaJS 与打字稿一起使用,但遇到了一些问题。 When I try to run my package-1 which has package-2 as dependency I get the error:当我尝试运行以 package-2 作为依赖项的 package-1 时,出现错误:

module.js:549
        throw err;
        ^
    
    Error: Cannot find module 'package-2'
        at Function.Module._resolveFilename (module.js:547:15)
        at Function.Module._load (module.js:474:25)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object. (/home/gabriel/Documentos/projetos/nodejs/lerna-t2/packages/package-1/dist/index.js:3:19)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)

I've followed the following steps:我遵循了以下步骤:

  1. I've created a directory and ran the command 'lerna init'我创建了一个目录并运行了命令“lerna init”
  2. I've created 2 folders inside (package-1, package-2) 'packages' folder我在 (package-1, package-2) 'packages' 文件夹中创建了 2 个文件夹
.
    ├── lerna.json
    ├── node_modules
    ├── package.json
    ├── package-lock.json
    └── packages
        ├── package-1
        └── package-2
  1. I've ran 'npm init' in both (package-1, package-2)我已经在 (package-1, package-2) 中运行了“npm init”
  2. I've created a basic 'tsconfig.json' in both packages:我在两个包中都创建了一个基本的“tsconfig.json”:
{
        "compilerOptions": {
            "outDir": "dist",
            "target": "es5",
            "module": "commonjs"
        }
    }
  1. I've ran 'lerna bootstrap' in the root folder我在根文件夹中运行了“lerna bootstrap”
  2. I've use 'lerna add package-2 --scope=package-1'我使用了'lerna add package-2 --scope=package-1'
  3. I've ran also 'npm i' in root folder, package-1 and package-2我也在根文件夹、package-1 和 package-2 中运行了“npm i”
  4. I've ran 'tsc -w' in package-1 and package-2 and 'node dist/index.js' in package-1我在 package-1 和 package-2 中运行了“tsc -w”,在 package-1 中运行了“node dist/index.js”

FILE TREE文件树

My 'package-1' file tree:我的“package-1”文件树:

.
    ├── dist
    │   └── index.js
    ├── index.ts
    ├── node_modules
    │   ├── package-2 -> ../../package-2
    │   └── typescript
    ├── package.json
    ├── package-lock.json
    └── tsconfig.json

My 'package-2' file tree:我的“package-2”文件树:

.
    ├── dist
    │   ├── index.js
    │   └── lib
    │       └── teste.js
    ├── index.ts
    ├── lib
    │   └── teste.ts
    ├── package.json
    ├── package-lock.json
    └── tsconfig.json

CODE代码

package-1 :包 1

  • index.ts:索引.ts:
import { Teste } from 'package-2'
    
    new Teste().printHello()

package-2 :包 2

  • lib/teste.ts:库/teste.ts:
export class Teste {
    
        printHello() {
            console.log('Hello!')
        }
    
    }
  • index.ts:索引.ts:
export { Teste } from './lib/teste'

Please ensure you have defined main in your package.json in (at least project-2).请确保您在 package.json 中定义了main (至少是 project-2)。 I assume the name field in package.json (for project-2) is project-2 .我假设 package.json(对于 project-2)中的 name 字段是project-2

Main should link to dist/index.js Main 应该链接到dist/index.js

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

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