如何在Webpack 4中使用ES6的import/export语法?

11

我正在尝试使用Webpack 4实现基本的ES6 import/export,但是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

Makefile:

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中的导入路径变化,但它们都得到相同的结果:无法解析模块文件。我错过了什么?

2个回答

8
你需要使用babel进行转译,使用babel-loader
此外,你不需要使用make,只需使用npm scripts即可。
存在错误,例如导入import { hello } from 'hello.js',应该是import { hello } from './hello.js'或者没有.js,像这样import { 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 build或者npm run prod,然后再运行npm run start,这将会记录输出结果。


1
只需要加上 ./ (例如 import { hello } from "./hello.js")就可以了。谢谢! - user574835783
不久之后,大约一个月左右,Webpack 4 将被广泛使用,并且 babel-preset-env 将是您所需要的全部内容,因此不要使用旧的实践方法,因为很快它将会改变。像我一样上传您的代码。 - deadcoder0904

3
现代解决方案是使用 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,然后通过在 package.json 脚本命令中添加 webpack --config webpack.config.cjs 来告知 webpack 新的配置文件。如果 webpack 修复了与 require 相关的问题,请留意https://github.com/webpack/webpack-cli/issues/1165 这意味着您不需要任何 babel 或编译器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接