網站搜尋

reactjs - Typescript Webpack React 編譯導致只讀檔案系統錯誤,但開發伺服器運作正常


我目前正在嘗試使用 React、Webpack 和 Typescript 編譯我的專案。我的開發伺服器可以運作,但當我嘗試執行 webpack 進行編譯時,出現以下情況:

> [email  build /Users/brighton1101/code/typescript_react
> webpack

Error: EROFS: read-only file system, mkdir '/dist'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email  build: `webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email  build script.

我的 webpack 配置看起來像這樣:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
  mode: "production",

  // Enable sourcemaps for debugging webpack's output.
  devtool: "source-map",

  resolve: {
    modules: ["components", "node_modules", "./"],
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: [".ts", ".tsx"]
  },

  "entry": {
    app: './index.tsx',
  },

  devtool: 'inline-source-map',

  module: {
    rules: [
      {
        test: /\.ts(x?)$/,
        //exclude: /node_modules/,
        use: [
          {
            loader: "ts-loader"
          }
        ]
      },
      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      {
        enforce: "pre",
        test: /\.js$/,
        loader: "source-map-loader"
      }
    ]
  },

  // When importing a module whose path matches one of the following, just
  // assume a corresponding global variable exists and use that instead.
  // This is important because it allows us to avoid bundling all of our
  // dependencies, which allows browsers to cache those libraries between builds.
  externals: {
    'react': "React",
    'react-dom': "ReactDOM"
  },

  plugins: [
    new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }),
    new HtmlWebpackPlugin({
      title: 'Development',
      template: './index.html'
    }),
  ],

  output: {
    filename: 'bundle.js',
    path: '/dist',
    publicPath: '/dist'
  },

  devServer: {
    contentBase: './',
    compress: true,
    port: 9000
  }
};

我的 Typescript 配置:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react"
  }
}

我不確定為什麼會出現這個錯誤,但我相當確定這與我某種配置錯誤有關。

答案#1

你的 output 設定不正確,它指向了你的根目錄。
試試這個:

const path = require('path');

output: {
  filename: 'bundle.js',
  path: path.resolve(__dirname, 'dist'),
  publicPath: path.resolve(__dirname, 'dist')
},