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

Nat*_*437 11 create-react-app

我正在尝试为我的团队创建一个自定义 cra 模板文件以供使用,但一直遇到一个问题,即我在运行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]'
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

我是否必须将模板发布到公共 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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

和 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"
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

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

Nat*_*437 17

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

更改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"
  ]
}

Run Code Online (Sandbox Code Playgroud)

  • 只有两种类型的日子:当你在花费数小时排除问题之前找到解决问题的 Stack Overflow 答案的日子,以及像今天这样的日子。 (2认同)