Can't resolve 'css-loader' - Webpack Getting Started Error - css

Following exactly the getting started guide on the webpack site I run into this error on build:
ERROR in ./src/index.js Module not found: Error: Can't resolve 'css-loader' in 'C:\Users\Dominik\Desktop\tutorial' # ./src/index.js
2:0-21 webpack.config.js
The css-loader is installed locally. It is registered in the package.json. What Im I doing wrong?
const path = require("path");
module.exports = {
entry: "./src/index.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname, "dist")
},
module: {
rules: [
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
}
};
package.json
{
"name": "tutorial",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^3.4.0",
"style-loader": "^1.1.1",
"webpack": "^4.41.4",
"webpack-cli": "^3.3.10"
},
"dependencies": {
"lodash": "^4.17.15"
}
}
index.js
import _ from "lodash";
import "./style.css";
function component() {
const element = document.createElement("div");
element.innerHTML = _.join(["Hello", "webpack"], " ");
element.classList.add("hello");
return element;
}
document.body.appendChild(component());
style.css
.hello {
color: red;
}

Try re-running npm install --save-dev css-loader. Can you see it in your node_modules folder?
EDIT
Uninstalling and re-installing the module fixed it:
npm remove css-loader && npm install --save-dev css-loader

Related

Webpack Dev Server stops on error originated in postcss-loader

I'm using webpack-dev-server for development but after upgrading to webpack 5 it just stops when postcss-loader can't parse css file.
I start dev server via npx webpack-dev-server and when I start typing new rule in .css webpack tries to rebuild my app but since I haven't finished typing, file contains incorrect css rule. So build fails due to postcss-loader throwing error CssSyntaxError. However that makes webpack-dev-server stop completely. Previously webpack-dev-server simply ignored failed builds and waited till webpack manages to successfully build my app.
I prepared git repo where this can be reproduced: https://github.com/s-shev/webpack-dev-server-stops
I'm using latest versions of webpack, webpack-cli and webpack-dev-server
I'd appreciate if anyone can say whether I'm doing something wrong or if it's a bug in webpack-dev-derver.
Here's my webpack.config:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const devMode = process.env.NODE_ENV !== "production";
module.exports = {
mode: devMode ? "development" : "production",
devtool: devMode ? "eval-cheap-module-source-map" : "source-map",
entry: path.resolve(__dirname, "./src/index"),
devServer: {
host: "0.0.0.0",
port: 9000,
},
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: { loader: "ts-loader" },
},
{
test: /\.css$/,
exclude: /node_modules/,
use: ["style-loader", "css-loader", "postcss-loader"],
},
],
},
resolve: {
extensions: [".ts", ".tsx", ".js"],
modules: ["./node_modules"],
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, "public", "index.html"),
}),
],
};
and pacakge.json:
{
"name": "webpack-dev-server-stops",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"start": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#types/react": "^18.0.15",
"#types/react-dom": "^18.0.6",
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"postcss-loader": "^7.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"style-loader": "^3.3.1",
"ts-loader": "^9.3.1",
"typescript": "^4.7.4",
"typescript-plugin-css-modules": "^1.3.1",
"webpack": "^5.73.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.9.3"
}
}
Turned out to be an issue in postcss. Updating postcss-loader to 7.0.1 fixed the issue.
https://github.com/webpack/webpack-dev-server/issues/4514

how to compile vue template in vuejs3

I want to develop a vue3 application and use rollup as the bundler. I wrote a vue sfc
<template>
<div>
{{message}}
</div>
</template>
<script>
export default {
name: 'TestComponent',
setup(){
const message = '123456';
return {
message
}
}
}
</script>
<style lang="scss" scoped>
.red{
color: red;
}
</style>
and import it in the entry js file.
import Test from './Test.vue';
export default function (Vue) {
Vue.component(Test.name, Test);
}
then i installed rollup-plugin-vue to compile vue sfc, postcss and sass to compile sass.
const path = require('path');
const inputPath = path.resolve(__dirname, './src/index.js');
const outputUMDPath = path.resolve(__dirname, './dist/datav.umd.bundle.js');
const outputESPath = path.resolve(__dirname, './dist/datav.es.bundle.js');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const babel = require('rollup-plugin-babel');
const json = require('rollup-plugin-json');
const vue = require('rollup-plugin-vue');
const postcss = require('rollup-plugin-postcss');
export default {
input: inputPath,
output: [{
file: outputUMDPath,
format: 'umd',
name: 'datav-bundle',
globals: {
vue: 'vue'
}
}, {
file: outputESPath,
format: 'es',
globals: {
vue: 'vue'
}
}],
plugins: [
resolve(),
commonjs(),
babel({
exclude: 'node_modules/**',
}),
json(),
vue(),
postcss({
plugins: []
})
],
external: [
'vue'
]
}
Then IDE reported an error:
enter image description here
I installed #vue/compiler-sfc as well.
"#vue/compiler-sfc": "^3.0.6",
but the template was still not compiled.
D:\LAB\DataVisualization\libs\src\index.js → dist\datav.umd.bundle.js, dist\datav.es.bundle.js...
[!] (plugin commonjs) SyntaxError: Unexpected token (2:4) in D:\LAB\DataVisualization\libs\src\Test.vue?vue&type=template&id=07bdddea&lang.js
src\Test.vue?vue&type=template&id=07bdddea&lang.js (2:4)
1:
2: <div>
^
3: {{message}}
4: </div>
SyntaxError: Unexpected token (2:4) in D:\LAB\DataVisualization\libs\src\Test.vue?vue&type=template&id=07bdddea&lang.js
at Object.pp$4.raise (D:\LAB\DataVisualization\libs\node_modules\rollup\dist\shared\rollup.js:15857:13)
at Object.pp.unexpected (D:\LAB\DataVisualization\libs\node_modules\rollup\dist\shared\rollup.js:13549:8)
at Object.pp$3.parseExprAtom (D:\LAB\DataVisualization\libs\node_modules\rollup\dist\shared\rollup.js:15256:10)
at Object.pp$3.parseExprSubscripts (D:\LAB\DataVisualization\libs\node_modules\rollup\dist\shared\rollup.js:15059:19)
is there anything i did wrong ?
here is my package.json
{
"name": "libs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "rollup -wc rollup.config.dev.js",
"build": "rollup -c rollup.config.dev.js",
"build:prod": "rollup -c rollup.config.prod.js"
},
"keywords": [],
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.13.8",
"#babel/preset-env": "^7.13.8",
"#vue/compiler-sfc": "^3.0.6",
"rollup": "^2.40.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-vue": "^6.0.0",
"sass": "^1.32.8",
"vue": "^3.0.6"
},
"dependencies": {
"sam-test-data": "0.0.5"
}
}
Oh, i think maybe i know the reason, i installed rollup-plugin-vue#6.0.0-beta.6 instead of *#6.0.0, compiled succefully!

Cannot find module 'autoprefixer' when running npx tailwindcss init -p command

I'm using Vue 3 and trying to add tailwindcss into it from the following tutorial. https://tailwindcss.com/docs/guides/vue-3-vite#install-tailwind-via-npm
I have installed the dependencies using the following command,
npm install -D tailwindcss#npm:#tailwindcss/postcss7-compat postcss#^7 autoprefixer#^9
But when I tried to create the configuration files using the following command
npx tailwindcss init -p
It is giving me the following error.
npx: installed 83 in 5.2s Cannot find module 'autoprefixer' Require
stack:
/~/.npm/_npx/33283/lib/node_modules/tailwindcss/lib/cli/commands/build.js
/~/.npm/_npx/33283/lib/node_modules/tailwindcss/lib/cli/commands/index.js
/~/.npm/_npx/33283/lib/node_modules/tailwindcss/lib/cli/main.js
/~/.npm/_npx/33283/lib/node_modules/tailwindcss/lib/cli.js
I don't know why autoprefixer is not detecting because I have already installed it.
Even the package.json have it.
{
"name": "wooclime",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^3.0.0"
},
"devDependencies": {
"#vue/cli-plugin-babel": "~4.5.0",
"#vue/cli-plugin-eslint": "~4.5.0",
"#vue/cli-service": "~4.5.0",
"#vue/compiler-sfc": "^3.0.0",
"autoprefixer": "^9.8.6",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0-0",
"postcss": "^7.0.35",
"tailwindcss": "npm:#tailwindcss/postcss7-compat#^2.0.2"
},
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/vue3-essential",
"eslint:recommended"
],
"parserOptions": {
"parser": "babel-eslint"
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
]
}
Run:
npx tailwindcss-cli#latest init -p
Please uninstall run this command:
npm uninstall tailwindcss postcss autoprefixer
After this module is uninstall, please run this command:
npm install tailwindcss#latest postcss#latest autoprefixer#latest
I had the same problem when I ran the command with node version 14.15.0.
Apparently using node 15.5 solved it for me. I think there's a problem with npm or something.
You could use the following command using the tailwindcss cli with latest version an the flag --postcss or -p
npx tailwindcss-cli#latest init --postcss
Or follow these steps :
After installing that dependencies try to create the following files project root without running that command :
tailwind.config.js
module.exports = {
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
};
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}
}
Then in your main CSS file add :
#tailwind base;
#tailwind components;
#tailwind utilities;
I had same issue
I downgrade the tailwindcss package to 1.0.5 from latest
Now it is working

sass-loader doesn't generate css

I tried to update this webpack config to generate css from scss. I added styles.scss into src folder with one css rule:
body{
font-size: 38px;
}
If I run yarn build or yarn dev, webpack doesn't generate any css files and I don't get any error.
webpack.config.js
/* global __dirname, require, module*/
const webpack = require('webpack');
const path = require('path');
const env = require('yargs').argv.env; // use --env with webpack 2
const pkg = require('./package.json');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
let libraryName = pkg.name;
let outputFile, mode;
if (env === 'build') {
mode = 'production';
outputFile = libraryName + '.min.js';
} else {
mode = 'development';
outputFile = libraryName + '.js';
}
const config = {
mode: mode,
entry: __dirname + '/src/index.js',
devtool: 'inline-source-map',
output: {
path: __dirname + '/lib',
filename: outputFile,
library: libraryName,
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: "typeof self !== 'undefined' ? self : this"
},
module: {
rules: [
{
test: /(\.jsx|\.js)$/,
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/
},
{
test: /(\.jsx|\.js)$/,
loader: 'eslint-loader',
exclude: /node_modules/
},
{
test: /\.scss$/,
use: [
// fallback to style-loader in development
process.env.NODE_ENV !== 'production' ? 'style-loader' : MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
},
resolve: {
modules: [path.resolve('./node_modules'), path.resolve('./src')],
extensions: ['.json', '.js', '.scss']
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css"
})
]
};
module.exports = config;
package.json
{
"name": "webpack-library-starter",
"version": "1.0.2",
"description": "Produce universal library with webpack and es6",
"main": "lib/webpack-library-starter.js",
"scripts": {
"build": "webpack --env dev && webpack --env build && npm run test",
"dev": "webpack --progress --colors --watch --env dev",
"test": "mocha --require babel-register --colors ./test/*.spec.js",
"test:watch": "mocha --require babel-register --colors -w ./test/*.spec.js",
"test:cover": "cross-env NODE_ENV=test nyc mocha --require babel-register --colors test/*.js",
"repl": "node -i -e \"$(< ./lib/webpack-library-starter.js)\""
},
"repository": {
"type": "git",
"url": "https://github.com/krasimir/webpack-library-starter.git"
},
"keywords": [
"webpack",
"es6",
"starter",
"library",
"universal",
"umd",
"commonjs"
],
"author": "Krasimir Tsonev",
"license": "MIT",
"bugs": {
"url": "https://github.com/krasimir/webpack-library-starter/issues"
},
"homepage": "https://github.com/krasimir/webpack-library-starter",
"devDependencies": {
"#babel/cli": "^7.0.0-beta.51",
"#babel/core": "^7.0.0-beta.51",
"#babel/preset-env": "^7.0.0-beta.51",
"babel-eslint": "^8.0.3",
"babel-loader": "^8.0.0-beta.4",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-istanbul": "^5.1.0",
"babel-preset-env": "^7.0.0-beta.3",
"babel-register": "^7.0.0-beta.3",
"chai": "^4.1.2",
"cross-env": "^5.2.0",
"eslint": "^5.0.1",
"eslint-loader": "^2.0.0",
"jsdom": "11.11.0",
"jsdom-global": "3.0.2",
"mocha": "^4.0.1",
"nyc": "^13.1.0",
"uglifyjs-webpack-plugin": "^1.2.7",
"webpack": "^4.12.2",
"webpack-cli": "^3.0.8",
"yargs": "^10.0.3"
},
"nyc": {
"sourceMap": false,
"instrument": false
},
"dependencies": {
"css-loader": "^2.1.1",
"mini-css-extract-plugin": "^0.7.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1"
}
}
Check, if you imported scss file in entry js file, for example index.js.

Grunt - Task "assemble" not found

Haven been playing with this for several hours
package.json
{
"name": "compile-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"grunt": "^0.4.5",
"assemble": "^0.7.3"
}
}
Gruntfile.js
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
assemble: {
options: {
flatten: true,
layout: "../templates/1.hbs"
},
pages: {
files: {
'../dist' : ['../pages/*.hbs']
}
}
}
});
grunt.loadNpmTasks('assemble');
grunt.registerTask('default', ['assemble']);
};
Tried removing and re-installing all the modules a few times. And also tried different versions, interestingly the versions mentioned in this article http://blog.parkji.co.uk/2013/07/06/building-a-static-site-using-grunt-and-assemble.html seem to work but then I get other issues because those versions are out of date.
Error
$ Grunt
>> Local Npm module "assemble" not found. Is it installed?
Warning: Task "assemble" not found. Used --force, continuing.
I just had to use grunt-assemble
$ npm install grunt-assemble --save-dev
And adjust this line in the Gruntfile.js
grunt.loadNpmTasks('grunt-assemble');

Resources