我正在尝试创建供我的团队使用的自定义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]'
]
}
目前模板本身非常简单,我认为这里的问题可能是我们的团队为一些内部软件包(模板中的依赖项)使用了私有供稿。此私有供稿设置为使用npm作为上游供稿。
我尝试使用该--template file:../path/to/template
方法在本地测试模板,并将模板发布到我们的内部Feed中。我可以使用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"
]
}
而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,但是我可以使用打字稿模板创建应用程序。
在对其他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"
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句