【问题标题】:How to use ES6 import/export with Webpack 4?如何在 Webpack 4 中使用 ES6 导入/导出?
【发布时间】:2018-08-19 20:03:26
【问题描述】:

我正在尝试让基本的 ES6 import/export 与 Webpack 4 一起使用,而 Webpack 似乎无法解析我的模块,尽管根据错误消息它正在查看它:

$ make
./node_modules/.bin/webpack --mode production src/app.js -o dist/bundle.js
Hash: 6decf05b399fcbd42b01
Version: webpack 4.1.1
Time: 339ms
Built at: 2018-3-11 14:50:58
 1 asset
 Entrypoint main = bundle.js
    [0] ./src/app.js 41 bytes {0} [built]

ERROR in ./src/app.js
Module not found: Error: Can't resolve 'hello.js' in '/home/(myhomedir)/code/js/src'
 @ ./src/app.js 1:0-31 2:0-5                                    

这是我的设置(node_modulesdist 等省略):

$ npm ls --depth=0
.
├── webpack@4.1.1
└── webpack-cli@2.0.11

$ tree
.
├── Makefile
└── src
    ├── app.js
    └── hello.js

生成文件:

webpack = ./node_modules/.bin/webpack --mode production
entry = src/app.js

webpack: $(entry)
    $(webpack) $(entry) -o dist/bundle.js

src/app.js:

import {hello} from "hello.js";
hello();

src/hello.js:

function hello() {
    alert("yes it's hello");
}
export { hello };

我在app.js 的导入路径上尝试了许多变体,但它们都得到相同的结果:Can't resolve 模块文件。我错过了什么?

【问题讨论】:

    标签: webpack es6-modules webpack-4


    【解决方案1】:

    您需要使用 babel 转译它并使用 babel-loader

    另外,您不需要使用 ma​​ke,只需使用 npm 脚本

    存在错误,例如导入 import { hello } from 'hello.js' 而应该是 import { hello } from './hello.js' 或没有 .jsimport { hello } from './hello'

    试试下面的 -

    npm install --save-dev babel-core babel-loader babel-preset-env webpack@next webpack-cli

    src/app.js

    import { hello } from "./hello";
    hello();
    

    src/hello.js

    function hello() {
      console.log("yes it's hello");
    }
    export { hello };
    

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/app.js",
    
      output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js"
      },
    
      module: {
        rules: [
          {
            test: /\.js$/,
            loader: "babel-loader",
            exclude: /(node_modules)/
          }
        ]
      }
    };
    

    package.json

    {
      "name": "webpack-test",
      "version": "1.0.0",
      "description": "",
      "main": "webpack.config.js",
      "scripts": {
        "build": "webpack --mode development",
        "prod": "webpack --mode production",
        "start": "node dist/bundle.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "babel-core": "^6.26.0",
        "babel-loader": "^7.1.4",
        "babel-preset-env": "^1.6.1",
        "webpack": "^4.0.0-beta.3",
        "webpack-cli": "^2.0.11"
      }
    }
    

    .babelrc

    {
      "presets": ["env"]
    }
    

    首先运行npm run buildnpm run prod,然后运行npm run start,这将记录输出

    【讨论】:

    • ./ (如 import { hello } from "./hello.js")就是它所需要的。谢谢!
    • 如果正确,请点赞。也很快 webpack 4 将在一个月左右使用 & babel-preset-env 将是你所需要的,所以不要使用旧的做法,因为它很快就会改变并上传你像我一样的代码
    【解决方案2】:

    现代解决方案是使用 node.js 13+,然后将 "type": "module" 添加到您的 package.json 文件中。这将允许您使用 import 语法而不是 require。更多信息在这里https://nodejs.org/dist/latest-v13.x/docs/api/esm.html

    Webpack 仍然需要 require 语法,因此您必须将 webpack.config.js 重命名为 webpack.config.cjs,然后通过将 webpack --config webpack.config.cjs 添加到您的 package.json 脚本命令让 webpack 知道新的配置文件是什么。如果 webpack 用 require 解决了他们的问题,请留意 https://github.com/webpack/webpack-cli/issues/1165

    这意味着您不需要任何 babel 或任何转译器。

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2018-07-11
      • 2016-07-30
      • 1970-01-01
      相关资源
      最近更新 更多