Purgecss: Cannot find module - config file not found

Created on 11 Sep 2018  路  9Comments  路  Source: FullHuman/purgecss

Describe the bug
When I try to run my build I'm getting an error that the config file can't be found.

To Reproduce
Steps to reproduce the behavior:

  1. Put a new script in your package.json: "purgecss": "purgecss -c purgecss.config.js -o src"
  2. Add purgecss.config.js file with the following contents: module.exports = { content: ['src/index.js'], css: ['src/tailwind.css'] }
  3. Type yarn run build
  4. See error message: Error: Error loading the config fileCannot find module '/C:\GitHub\repo/purgecss.config.js

Expected behavior
Build should happen with css purged

Desktop (please complete the following information):

  • OS: Windows 10
  • Version of Purgecss: 1.1.0

Additional context
This was raised on StackOverflow as well: https://stackoverflow.com/questions/51569213/purgecss-cli-c-cannot-find-module-purgecss-config-js/52277041

bug

Most helpful comment

Ill have to first setup a VM with windows to test this but a possible workaround would be to just not use a config file and use purgecss programmatically. Create a purge_my_css.js file with something like this:

const Purgecss = require('purgecss')
const fs = require('fs')
const path = require('path')

const outputDir = path.resolve(__dirname, 'dist')

const purgecss = new Purgecss({
  content: ['src/*.html'],
  css: ['src/*.css']
})
const result = purgecss.purge()

result.forEach(out => {
    const filePath = out.file.split('/')
    fs.writeFileSync(`${outputDir}/${filePath[filePath.length - 1]}`, out.css, 'utf-8')
})

console.log('done')

then just run it with node purge_my_css.js

All 9 comments

thanks for reporting this, it looks like a bug on windows, in this path /C:\GitHub\repo/purgecss.config.js the last slash should be a backslash and i'm not sure if that first slash before C: should be there

Agreed, that's what I commented on the StackOverflow bug too. Hopefully it's not too bad to fix.

Ill have to first setup a VM with windows to test this but a possible workaround would be to just not use a config file and use purgecss programmatically. Create a purge_my_css.js file with something like this:

const Purgecss = require('purgecss')
const fs = require('fs')
const path = require('path')

const outputDir = path.resolve(__dirname, 'dist')

const purgecss = new Purgecss({
  content: ['src/*.html'],
  css: ['src/*.css']
})
const result = purgecss.purge()

result.forEach(out => {
    const filePath = out.file.split('/')
    fs.writeFileSync(`${outputDir}/${filePath[filePath.length - 1]}`, out.css, 'utf-8')
})

console.log('done')

then just run it with node purge_my_css.js

I've fixed the issue on my end by simply replacing

var t = path.resolve(process.cwd(), pathConfig);
with
var t = path.join(process.cwd(), pathConfig);

I'm assuming since process.cwd() returns a fixed path, using join over resolve is a non-issue, but I'm not in the position to make PR's or tests at the moment.

Maybe someone else can research if this change would cause any issues?

The fix from @alexqhj worked in my windows machine.

I finally got this to work by putting a symlink to the node_modules directory (named "node_modules") in each directory where I was running purgecss from.

This problem still exists under windows.

image

i just changed it directly in the purgecss file

image

and apprently the developpers know the problem

Was this page helpful?
0 / 5 - 0 ratings

Related issues

XhmikosR picture XhmikosR  路  9Comments

simplenotezy picture simplenotezy  路  3Comments

ctf0 picture ctf0  路  4Comments

claudiodekker picture claudiodekker  路  4Comments

knowler picture knowler  路  3Comments