找不到模块:在 React 应用程序中本地安装 npm 包时无法解析



构建完成后,使用npm i ../将npm包安装到我新创建的react应用程序中。然后在App.js中导入组件。但是它显示了Module没有找到的错误:Can't resolve 'test-app-npm'。但是它是安装在包里的。Json像这样"test-app-npm"; "file:../testnpm"

创建的包文件夹名称:"testnpm"创建react app文件夹名称:"testapp">

webpack.config.js

var path = require("path");
module.exports = {
mode: "production",
entry: "./src/index.js",
output: {
path: path.resolve("build"),
filename: "index.js",
libraryTarget: "commonjs2",
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
loader: "babel-loader",
},
{
test: /.css$/,
use: ["style-loader", "css-loader"],
},
],
},
externals: {
react: "react",
},
};

package.json

{
"name": "test-app-npm",
"version": "1.0.0",
"description": "test react app npm release",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"build": "webpack"
},
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-react": "^7.16.0",
"babel-loader": "^8.2.3",
"css-loader": "^6.5.0",
"style-loader": "^3.3.1",
"webpack": "^5.61.0",
"webpack-cli": "^4.9.1"
},
"dependencies": {
"react": "^17.0.2"
},
"author": "",
"license": "ISC"
}

更改包。Json "main";/build/index.js"它的工作原理. .

相关内容

  • 没有找到相关文章

最新更新