node.js - Webpack 配置不起作用,路径必须是绝对路径

标签 node.js reactjs configuration webpack

我的 Webpack 配置出现错误:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.

  • configuration.output.path: The provided value "./" is not an absolute path!

这是我的webpack.config.js:

var config = {
   entry: './main.js',
 
   output: {
      path:'./',
      filename: 'index.js',
   },
 
   devServer: {
      inline: true,
      port: 8080
   },
 
   module: {
      loaders: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel',
       
            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   }
}

module.exports = config;

最佳答案

您的路径必须是绝对的,而不是相对的。要使用当前目录,请使用变量 __dirname :

__dirname

The directory name of the current module. This the same as the path.dirname() of the __filename.

__dirname is not actually a global but rather local to each module.

Example: running node example.js from /Users/mjr

console.log(__dirname);
// Prints: /Users/mjr
console.log(path.dirname(__filename));
// Prints: /Users/mjr

因此,它是您当前的目录,配置以绝对路径形式存储在其中,您可以像这样应用它:

output: {
   path: __dirname,
   filename: 'index.js',
},

(请注意 ./__dirname 之间存在差异,如 question 中所述。./ 指当前目录调用脚本的终端的名称,__dirname 指的是脚本存储的目录。)

关于node.js - Webpack 配置不起作用,路径必须是绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44125310/

相关文章:

reactjs - 如何在 firebase.auth().currentUser 上异步/等待?

configuration - Azure DevOps - Azure 资源组部署 : set value of subscription dynamically

postgresql - 我应该从 PostgreSQL search_path 中删除 "$user"吗?

javascript - 如何在node js xlsx npm中填充行的背景颜色

javascript - 我怎样才能检测到我的 Node.js 脚本被永远终止了?

node.js - 如何找出NPX运行的文件?

node.js - Azure Pipelines 中的变量可以用在 NodeJS 代码中吗?

javascript - 为 React Component Props 解构 ImmutableJS Map

ReactJS 动态地将键添加到混合内容数组中

java - 在已部署的 java war 文件之外配置文件是否安全?