yaml: Failed to use yaml lib with webpack 5

Got an error during webpack server command. And I can’t figure out what I do wrong. If possible could you please help?

> webpack serve

ℹ 「wds」: Project is running at http://localhost:3333/
ℹ 「wds」: webpack output is served from undefined
ℹ 「wds」: Content not from webpack is served from /Users/igor/Projects/yaml-test/src
✖ 「wdm」: 1 asset
46 modules

ERROR in ./node_modules/yaml/browser/dist/index.js 1:0
Module parse failed: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> import { d as defaultTagPrefix, _ as _createForOfIteratorHelper, a as _typeof, b as _createClass, c as _classCallCheck, e as _defineProperty, Y as YAMLSyntaxError, T as Type, f as YAMLWarning, g as YAMLSemanticError, h as _slicedToArray, i as YAMLError, j as _inherits, k as _createSuper } from './PlainValue-ff5147c6.js';
| import { parse as parse$1 } from './parse-cst.js';
| import { b as binaryOptions, a as boolOptions, i as intOptions, n as nullOptions, s as strOptions, N as Node, P as Pair, S as Scalar, c as stringifyString, A as Alias, Y as YAMLSeq, d as YAMLMap, M as Merge, C as Collection, r as resolveNode, e as isEmptyPath, t as toJSON, f as addComment } from './resolveSeq-04825f30.js';

webpack 5.3.2 compiled with 1 error in 4332 ms
ℹ 「wdm」: Failed to compile.

Webpack config:

const HtmlWebpackPlugin = require('html-webpack-plugin')

const path = require('path');

const srcDir = path.resolve(__dirname, 'src');

module.exports = {
  entry: `${srcDir}/index.js`,
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[contenthash].js',
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({template: 'src/index.html'}),
  ],
  devServer: {
    port: 3333,
    contentBase: srcDir,
    stats: 'minimal',
    hot: true,
    inline: true,
  },
};

package.json

{
  "name": "yaml-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "yaml": "^1.10.0"
  },
  "devDependencies": {
    "@babel/core": "^7.12.3",
    "@babel/preset-env": "^7.12.1",
    "@webpack-cli/serve": "^1.0.1",
    "babel-loader": "^8.1.0",
    "html-webpack-plugin": "^4.5.0",
    "webpack": "^5.3.2",
    "webpack-cli": "^4.1.0",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "start": "webpack serve"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

index.js

import YAML from 'yaml';

console.log(YAML.parse('1'));

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I thought you suggest to add /node_modules\/yaml\/browser\/dist\/.*/ to exclude (read inattentively 😄). But then I realize that I need to change rule test property. So in my work project I just add another rule to handle yamlspecifically.

{
  test: /node_modules\/yaml\/browser\/dist\/.*/,
  type: 'javascript/auto',
  use: {
    loader: 'babel-loader',
    options: {
      presets: [
        '@babel/preset-env',
      ],
    },
  },
}

Thanks

Huh, looks like Webpack 5 cares about the package.json "type" field, which it didn’t before. Such will need to be added to the browser/dist/ directory of yaml, but in the meantime (or for yaml@1), adding this to your module.rules should make it work:

{ test: /\.js$/, type: 'javascript/auto' }

If you’d prefer to make the test really specific, use /node_modules\/yaml\/browser/.

Edit: Fixed specific regexp to grab all of yaml/browser.