next.js: import svg (path must be a string or Buffer)

for me this example doesn’t work https://github.com/zeit/next.js/tree/master/examples/svg-components

npm install babel-plugin-inline-react-svg --save-dev

.babelrc:

{
  "plugins": [
    [
      "module-resolver", {
        "root": ["."],
        "alias": {
          "styles": "./styles"
        },
        "cwd": "babelrc"
    }],
    [
      "wrap-in-js",
      {
        "extensions": ["css$"]
      }
    ],
    ["inline-react-svg"]
  ],
  "presets": [
    "next/babel"
  ],
  "ignore": []
}

and i get

Module build failed: 
TypeError: C:/Users/.../src/components/header/View.js: path must be a string or Buffer
    at TypeError (native)
    at Object.fs.openSync (fs.js:641:18)
    at fs.readFileSync (fs.js:509:33)
    at PluginPass.ImportDeclaration (C:\Users\...\node_modules\babel-plugin-inline-react-svg\lib\index.js:63:50)
    at newFn (C:\Users\...\node_modules\babel-traverse\lib\visitors.js:276:21)

tried with multiple sources of svg’s. There content looks fine

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Not one that I can share. Its on a private repo. But the use case is very simple. I used the static export example (https://github.com/zeit/next.js/tree/master/examples/with-static-export) as a boilerplate. Added the inline-svg plugin. I then used it like this

import { PureComponent } from "react";
import OutlineButton from "./outline-button/outline-button-component";
import componentStyles from "./navbar-component.css";
import MondayLogo from "../../../static/monday-logos/monday6-temp-logo.svg";

export default class NavbarComponent extends PureComponent {
  render() {
    return (
      <header>
        <nav className="navbar" id="navbar-fixed" role="navigation">
          <div className="homepage-logo">
            <MondayLogo />
          </div>
          <div className="navbar-menu">
            <OutlineButton className="navbar-login" url="https://auth.monday.com/login" text="Log In" theme="dark" />
            <div className="menu" />
          </div>
        </nav>
        <div className="navbar-padding" />
        <style jsx>{componentStyles}</style>
      </header>
    );
  }
}

Hope this helps

Strange, I already use relative paths and still have this issue…

https://www.npmjs.com/package/babel-plugin-module-resolver

i’m using the Linux subsystem on windows 10 and my app deployed via now also works fine