从自定义模板创建时找不到创建反应应用程序模板模块

Create react app template module not found when creating from custom template

我正在尝试创建一个自定义 cra-template 文件供我的团队使用,但在 运行 npx create-react-app test-app --template my-custom-template.

时我一直遇到以下错误
internal/modules/cjs/loader.js:796
    throw err;
    ^

Error: Cannot find module 'cra-template-my-custom-template'
Require stack:
- C:\Users\user\Desktop\cra-test-app\test-app\node_modules\react-scripts\scripts\init.js
- C:\Users\user\Desktop\cra-test-app\test-app\[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
    at Function.resolve (internal/modules/cjs/helpers.js:80:19)
    at module.exports (C:\Users\user\Desktop\cra-test-app\test-app\node_modules\react-scripts\scripts\init.js:110:13)
    at [eval]:3:14
    at Script.runInThisContext (vm.js:116:20)
    at Object.runInThisContext (vm.js:306:38)
    at Object.<anonymous> ([eval]-wrapper:9:26)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at evalScript (internal/process/execution.js:80:25)
    at internal/main/eval_string.js:23:3 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\Users\user\Desktop\cra-test-app\test-app\node_modules\react-scripts\scripts\init.js',
    'C:\Users\user\Desktop\cra-test-app\test-app\[eval]'
  ]
}

目前模板本身相当简单,我认为这里的问题可能是我们的团队对一些内部包(它们是模板中的依赖项)使用私有提要。此私人提要设置为使用 npm 作为上游提要。

我已尝试使用 --template file:../path/to/template 方法在本地测试模板并将模板发布到我们的内部提要。我可以使用 npm install 安装包,所以肯定可以在注册表中找到该包。

我还尝试按照其他一些建议刷新 npm 缓存并卸载 create-react-app。

我是否必须 将模板发布到 public npm 注册中心才能正常工作?还是我漏掉了什么?

我的模板的package.json文件如下:

{
  "name": "cra-template-my-custom-template",
  "version": "1.0.5",
  "keywords": [
    "react",
    "create-react-app",
    "template",
    "typescript"
  ],
  "description": "The base template for React apps hosted within our project",
  "files": [
    "template",
    "template.json"
  ]
}

而template.json如下:

{
    "package": {
      "dependencies": {
        "@<custom-feed-scope>/<custom-feed-library>": "^5.11.0",
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.5.0",
        "@testing-library/user-event": "^7.2.1",
        "@types/jest": "^24.9.1",
        "@types/node": "^12.12.34",
        "@types/react": "^16.9.32",
        "@types/react-dom": "^16.9.6",
        "<custom-feed-library>": "^3.2.1",
        "<custom-feed-library>": "^0.2.4",
        "typescript": "^3.7.5"
      }
    }
  }

我目前使用的是 npm 而不是 yarn,但我能够使用 typescript 模板创建一个应用程序就好了。

在对其他 cra-template 包进行一些研究后,似乎这里缺少的部分没有在我的模板的 package.json 文件中定义 main 值。

package.json 更改为以下内容后,它开始工作了。

{
  "name": "cra-template-my-custom-template",
  "version": "1.0.8",
  "keywords": [
    "react",
    "create-react-app",
    "template",
    "typescript"
  ],
  "main": "template.json",
  "description": "The base template for React apps hosted within our project",
  "files": [
    "template",
    "template.json"
  ]
}