0

我有一个monorepo:

domain/
  entities/
    account.ts
  ...
mobile/
    src/
      app.ts
  node_modules/
  package.json
  babel.config.js

我想设置一个别名,以便app.ts可以简单地调用:

import { Account } from 'domain/entities/account'

代替

import { Account } from '../../../domain/entities/account'

我试着这样做:

const path = require('path')

module.exports = (api) => {
  api.cache(true)

  return {
    presets: ['module:metro-react-native-babel-preset'],
    plugins: [
      ['module:react-native-dotenv', {
        moduleName: 'react-native-dotenv'
      }],
      [
        'module-resolver',
        {
          extensions: [
            '.js',
            '.jsx',
            '.ts',
            '.tsx'
          ],
          alias: {
            domain: path.resolve(__dirname, '../domain')
          }
        }
      ]
    ]
  }
}

但不幸的是,它不起作用。它抛出:

Error: Unable to resolve module /home/kibe/work/iros-customer-frontend/domain/entities/account from ...

None of these files exist:
  * ../domain/entities/account(.js|.jsx|.ts|.tsx)

如何从主目录外部导入内容?

4

1 回答 1

0

转到“域”文件夹并添加具有以下内容的新文件 package.json

{
  "name": "domain"
}

现在你像这样导入

import { Account } from 'domain/entities/account'
于 2021-08-16T14:10:03.060 回答