簡體   English   中英

找不到模塊:在 React App 中本地安裝 npm 包時無法解析

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM