Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error resolving a CSS file import with custom Webpack rule #627

Closed
kpuputti opened this issue Jan 2, 2017 · 15 comments
Closed

Error resolving a CSS file import with custom Webpack rule #627

kpuputti opened this issue Jan 2, 2017 · 15 comments
Assignees

Comments

@kpuputti
Copy link

kpuputti commented Jan 2, 2017

Hi!

I'm (eventually) attempting to build a PostCSS/cssnext setup on top of Next.js, but somehow importing a CSS file cannot be resolved.

My pages/index.js file tries to import the pages/index.css file (that exists):

import './index.css'

With the Webpack config:

next.config.js:

module.exports = {
  webpack(config) {
    config.module.rules.push({
      test: /\.css$/,
      exclude: /node_modules/,
      loader: ['style-loader', 'css-loader']
    });
    return config;
  }
};

results in

output:

yarn run v0.15.1
$ next
> Using "webpack" config function defined in next.config.js.
 DONE  Compiled successfully in 3004ms

> Ready on http://localhost:3000
{ Error: Cannot find module './index.css'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/kpuputti/code/projects/nextlatest/pages/index.js?entry:3:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3) code: 'MODULE_NOT_FOUND' }

Package versions in package.json:

  "dependencies": {
    "css-loader": "^0.26.1",
    "next": "2.0.0-beta.12",
    "style-loader": "^0.13.1"
  }

Do I need to do some extra setup to get the resolving work, or how should I approach this? Thanks!

@Chathula
Copy link

Chathula commented Jan 2, 2017

https://github.com/css-modules/css-modules-require-hook

as i heard this works perfectly!

@joelburget
Copy link

I have a similar issue with raw importing markdown.

next.config.js

module.exports = {
  webpack: (config, { dev }) => {
    config.module.rules.push({ test: /\.md$/, loader: 'raw-loader' });
    return config;
  }
}

code

import React from 'react';

import render from '../md/renderer';
import Wrapper from '../components/Wrapper';

import content from './index.md';

export default () => (
  <Wrapper>
    {render(content)}
  </Wrapper>
)

error

> Ready on http://localhost:3000
{ Error: Cannot find module './index.md'
    at Function.Module._resolveFilename (module.js:472:15)
    at Function.Module._load (module.js:420:25)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/joel/sites/joelburget.com/pages/index.js?entry:6:1)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)
    at Function.Module._load (module.js:441:3) code: 'MODULE_NOT_FOUND' }

@joelburget
Copy link

As an additional data point, if I load the page in the browser (showing the error), then edit my source with any change, including adding a blank line, the page live reloads and shows the expected content. But when I reload I get the error page again.

@joelburget
Copy link

Also, I was running 2.0.0-beta.0 when I first saw this error and have since upgraded to 2.0.0-beta.9 and still see it.

@ovidiuch
Copy link
Contributor

ovidiuch commented Jan 3, 2017

It seems like the problem is an imparity between server and client side rendering. You add a webpack loader for some new file type but it doesn't apply on the first render (because SSR doesn't involve bundling via webpack, right?), so it doesn't work. But if you manage to re-render on the client (either by following a link or triggering HMR) it works.

If this is correct, the whole extending your webpack config to add additional loaders use case is flawed, because SSR is at the core of Next.js. @rauchg

The alternative is to rely on Babel transforms instead of webpack loaders, because those get applied consistently.

@arunoda
Copy link
Contributor

arunoda commented Jan 3, 2017

Guys, yes this is a real problem.
The real issue comes when we need to deal with the server side.
Since we don't(and can't) run webpack on the server, if a module introduce a new file extension to require, it'll be kind a hard to do.

Just like @skidding mentioned, babel plugins will be the solution for this. (At least in the short term)

@davidrossjones
Copy link
Contributor

@arunoda No way to get this working on the server? Something like this? https://github.com/halt-hammerzeit/universal-webpack

@arunoda
Copy link
Contributor

arunoda commented Jan 9, 2017

Thanks @davidrossjones. I have seen similar projects. But we need find a reliable but performant way to do it. Need to investigate into these tools.

@LarryLuTW
Copy link
Contributor

I have the same problems when I import the bootstrap css. Is there any solution?

@noeljackson
Copy link

Has any one tried using https://github.com/istarkov/babel-plugin-webpack-loaders with success?

@arunoda
Copy link
Contributor

arunoda commented Jan 16, 2017

@noeljackson That should be something interesting. I'd like to play with this.

@arunoda arunoda self-assigned this Jan 16, 2017
@noeljackson
Copy link

@arunoda It seems like babel-plugin-webpack-loaders should work, but then I run into an issue with loading pages and webpack-hot-middleware-client ... I tested this with Rauch's blog example of next.js This is the type of error I get.

Module build failed: TypeError: /Users/noel/n/optimized.technology/pages/2014/7-principles-of-rich-web-applications.js?entry: Path must be a string. Received undefined
    at assertPath (path.js:7:11)
    at Object.parse (path.js:1494:5)
    at Object.<anonymous> (/Users/noel/n/optimized.technology/webpack.config.js:16:35)
    at Module._compile (module.js:570:32)
    at loader (/Users/noel/n/optimized.technology/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/noel/n/optimized.technology/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)

 @ multi ./~/next/dist/client/webpack-hot-middleware-client ./pages/2014/7-principles-of-rich-web-applications.js?entry

@davibe
Copy link
Contributor

davibe commented Jan 31, 2017

Please, have a look at https://github.com/davibe/next.js-css-global-style-test and #544

@tgoldenberg
Copy link

@davibe I found that the repo you linked to loaded the css / scss but caused other issues. For example, I wasn't able to deploy successfully. Really wish there was better support for css imports in some way... And I don't get why <style jsx>{interpolatedStyles}</style> doesn't work. We should be able to use css as modules some way...

@timneutkens
Copy link
Member

This is actually a duplicate of #544. Closing this in favor of having the full CSS importing discussion there.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests