[英]Module not found: Can't resolve while installing npm package locally in React App
构建完成后,使用 npm i ../ 将 npm 包安装到我新创建的 React 应用程序中。 并在 App.js 中导入组件。 但它显示模块未找到的错误:无法解析'test-app-npm'。 但它安装在 package.json 中,就像这样“test-app-npm”:“file:../testnpm”。
创建的包文件夹名称:“testnpm”,创建的反应应用文件夹名称:“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",
},
};
包.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"
}
更改 package.json "main": "./build/index.js" 它有效..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.