For several days, I am trying to install webpack with React. I used several tutorials but I can' t still install it. Recently, I used this tutorial but in the last step, when I try to run webpack I get an error:
The CLI moved into a separate package: webpack-cli.
Please install 'webpack-cli' in addition to webpack itself to use the CLI.
-> When using npm: npm install webpack-cli -D
-> When using yarn: yarn add webpack-cli -D
Then, I run npm install webpack-cli -D, but it doesn't help and I don't know how to resolve that problem. When I try to only install webpack I receive the same error. Thanks for advices.
package.json and webpack.config.js content:
{
"name": "proj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"#types/react": "^16.0.40",
"#types/react-dom": "^16.0.4",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"devDependencies": {
"awesome-typescript-loader": "^3.5.0",
"source-map-loader": "^0.2.3",
"typescript": "^2.7.2",
"webpack": "^3.0.0",
"webpack-cli": "^2.0.10"
}
}
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "bundle.js",
path: __dirname + "/dist"
},
// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"]
},
module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },
// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" }
]
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
};
Ok I solve my problem. First of all, I deleted "webpack-cli": "^2.0.10" from my package.json. Then I install npm install webpack#3.0.0 and add to package.json: "scripts": {
"build": "webpack --config webpack.config.js"
}, Now I can run my webpack by npm run-script build. Thanks a lot.
Try install Webpack^4.1.0, webpack-cli^2.0.10
npm i --save-dev webpack#4.1.0 webpack-cli#2.0.10
Related
I'm a new dev but have a few projects under my belt. I'm stuck on a peculiar issue and I don't know where to start looking for a solution. Whenever I try to run "npm run build-dev", I get 38 error messages such as:
ERROR in ./src/index.js 1:13-28 Module not found: Error: Can't resolve 'path' in '/Users/******/src'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default. This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
- install 'path-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: { "path": false }
However, my index.js has only one line:
const express = require('express');
Did I add anything in package.json or webpack.dev.js that could create dozens of can't resolve error messages? I'm quite confused as I can't initialize the project.
Thank you very much for your help,
Matt
package.json
{
*******
"scripts": {
"start": "node dist/bundle.js",
"build-dev": "webpack --config webpack.dev.js",
"build-prod": "webpack --config webpack.prod.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "",
"description": "",
"dependencies": {
"axios": "^0.27.2",
"dotenv": "^16.0.2",
"express": "^4.18.1"
},
"devDependencies": {
"#types/node": "^18.7.18",
"babel-loader": "^8.2.5",
"clean-webpack-plugin": "^4.0.0",
"dotenv-webpack": "^8.0.1",
"ts-loader": "^9.4.0",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
}
}
webpack.dev.js
const webpack = require('webpack');
const path = require('path');
const Dotenv = require('dotenv-webpack');
module.exports = {
entry: './src/index.js',
mode: 'development',
/*resolve: {
extensions: [".ts", ".tsx", ".js"]
},*/
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader"
}
]
},
plugins: [
new Dotenv()
]
};
I'm having an unusual problem running a basic Framework7 project with vue.
Once created a Framework7 Vue project with the command: framework7 create --ui and installed the dependencies with npm i get the following problem: Could not resolve "vue"
in all packages because the script is looking for vue inside a esm folder like: ../../../node_modules/swiper/esm/vue/swiper.js:1:79 and it's quite strange because esm folder is never existed.
The strange thing is that the same project, with the same package.json works perfectly in another pc with the same version of node (v16.14.2) and npm (8.7.0)
So the problem must be in my pc. I thought about some cache problem, but despite forcing a cache cleanup the problem remains.
For completeness I leave the json package, which is the same that framework7 create --ui command creates for a Vue project.
Has anyone ever had a similar problem or know what the cause might be?
I guess the problem could be related to npm or vite.
{
"name": "my-app",
"private": true,
"version": "1.0.0",
"description": "My App",
"repository": "",
"license": "UNLICENSED",
"scripts": {
"start": "npm run dev",
"dev": "cross-env NODE_ENV=development vite",
"build": "cross-env NODE_ENV=production vite build && npx workbox generateSW workbox-config.js",
"build-capacitor-ios": "cross-env NODE_ENV=production vite build && npx cap copy ios",
"build-capacitor-android": "cross-env NODE_ENV=production vite build && npx cap copy android",
"postinstall": "cpy --flat ./node_modules/framework7-icons/fonts/*.* ./src/fonts/ && cpy --flat ./node_modules/material-icons/iconfont/*.* ./src/fonts/"
},
"browserslist": [
"IOS >= 13",
"Safari >= 13",
"last 5 Chrome versions",
"last 5 Firefox versions",
"Samsung >= 12"
],
"dependencies": {
"#capacitor/android": "^3.4.3",
"#capacitor/app": "^1.1.1",
"#capacitor/browser": "^1.0.7",
"#capacitor/core": "^3.4.3",
"#capacitor/ios": "^3.4.3",
"#capacitor/keyboard": "^1.2.2",
"#capacitor/splash-screen": "^1.2.2",
"#capacitor/status-bar": "^1.0.8",
"dom7": "^4.0.4",
"framework7": "^7.0.1",
"framework7-icons": "^5.0.5",
"framework7-vue": "^7.0.1",
"material-icons": "^1.10.8",
"skeleton-elements": "^4.0.0",
"swiper": "^8.1.1",
"vue": "^3.2.33"
},
"devDependencies": {
"#capacitor/cli": "^3.4.3",
"#vitejs/plugin-vue": "^2.3.1",
"#vue/compiler-sfc": "^3.2.33",
"cordova-res": "^0.15.4",
"cpy-cli": "^4.1.0",
"cross-env": "^7.0.3",
"postcss-preset-env": "^7.4.3",
"vite": "^2.9.5",
"workbox-cli": "^6.5.3"
}
}
Changing in vite.config.js works for me. I have used the node_modules path for all the external bundle related errors
You can mark the path "vue" as external to exclude it from the bundle, which will remove this error.
import path from 'path';
import vue from '#vitejs/plugin-vue';
const SRC_DIR = path.resolve(__dirname, './src');
const PUBLIC_DIR = path.resolve(__dirname, './public');
const BUILD_DIR = path.resolve(__dirname, './www',);
function getPath(new_path) {
return path.resolve(__dirname, 'node_modules/' + new_path);
}
export default {
plugins: [
vue(),
],
root: SRC_DIR,
base: '',
publicDir: PUBLIC_DIR,
build: {
outDir: BUILD_DIR,
assetsInlineLimit: 0,
emptyOutDir: true,
rollupOptions: {
treeshake: false,
},
},
resolve: {
alias: {
'#': SRC_DIR,
"vue": getPath('vue/dist/vue.runtime.esm-browser.js'),
"framework7/lite-bundle": getPath('framework7/framework7-lite-bundle.esm.js'),
"framework7-vue/bundle": getPath('framework7-vue/framework7-vue-bundle.js'),
"swiper/vue": getPath('swiper/vue/swiper-vue.js'),
"skeleton-elements/vue": getPath('skeleton-elements/vue/index.js'),
},
},
server: {
host: true,
},
};
I'm trying to re-build a react app and bundle it using webpack. Whenever I try to run npm run-script build it would always show a npm ERR! missing script: webpack
Things that I've tried:
Deleted the node_modules folder the doing a npm install (again)
installed the webpack globally
installed the webpack-cli
You may notice that I am using old version of some modules. This is because this is a legacy app built before I was employed and now I am trying to integrate the app that I built into this app but before that I would like to understand how the whole thing works as vanilla.
Below is my package.json file
{
"name": "storyline-feedback-tool",
"version": "1.1.2",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack",
"start-hot": "node server.js",
"start": "webpack-dev-server --content-base public/ --inline --port 3001"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-polyfill": "^6.13.0",
"classnames": "^2.2.5",
"g": "^2.0.1",
"lodash": "^4.15.0",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-spin": "^0.6.2",
"webpack": "^1.15.0",
"webpack-cli": "^3.2.3"
},
"devDependencies": {
"autoprefixer": "^6.4.0",
"babel-core": "^6.14.0",
"babel-eslint": "^6.1.2",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"babel-preset-react-hmre": "^1.1.1",
"css-loader": "^0.24.0",
"eslint": "^3.3.1",
"eslint-config-airbnb": "^10.0.1",
"eslint-plugin-import": "^1.14.0",
"eslint-plugin-jsx-a11y": "^2.2.0",
"eslint-plugin-react": "^6.1.2",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"postcss-loader": "^0.11.0",
"postcss-pxtorem": "^3.3.1",
"react-hot-loader": "^1.3.0",
"style-loader": "^0.13.1",
"webpack-dev-server": "^1.15.1"
}
}
Here is my webpack.config.js
var webpack = require('webpack');
var path = require('path');
var autoprefixer = require('autoprefixer');
var postcssPxtorem = require('postcss-pxtorem');
var config = {
name: 'feedback-tool',
entry: {
'feedback-main': './src/index-main.js',
'feedback-form': './src/index-form.js',
},
output: {
path: path.resolve(__dirname, 'public/feedback'),
publicPath: '/feedback/',
filename: '[name].js'
}
};
config.module = {
loaders: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /(node_modules)/,
loader: 'babel',
},
{
test: /\.less$/,
loader: "style-loader!css-loader!postcss-loader!less-loader"
},
{
test: /\.css$/,
loader: "style-loader!css-loader!postcss-loader"
}
]
};
config.postcss = function () {
return [
autoprefixer({
browsers: [
'last 2 version',
'ie > 8']
}),
postcssPxtorem({
propWhiteList: []
})]
};
config.plugins = [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
},
output: {
comments: false,
},
}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': '"production"'
}
}),
];
module.exports = config;
Any help would be appreciated.
in my case I just add to node "Scripts" the key "webpack":"webpack". and that worked for me.
{
"name": "reactdemo",
"version": "1.0.0",
"description": "lmora demo",
"main": "app.js",
"scripts": {
"webpack": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
The error message of: npm ERR! missing script: webpack makes me believe your webpack installation screwed up somewhere down the line or having webpack installed globally is causing issues due to the legacy nature of this project. Could you try the following and see if this resolves your issue:
Ensure that your version of node is compatible with webpack. The documentation recommends using the LTS version of Node: 10.15.1 LTS. Directly from webpack's documentation:
"Before we begin, make sure you have a fresh version of Node.js installed. The current Long Term Support (LTS) release is an ideal starting point. You may run into a variety of issues with the older versions as they may be missing functionality webpack and/or its related packages require."
Uninstall Webpack and the CLI:
npm uninstall -g webpack
npm uninstall webpack-cli
Re-Install webpack and the cli locally since installing webpack globally may cause issues:
"Note that this is not a recommended practice. Installing globally locks you down to a specific version of webpack and could fail in projects that use a different version."
npm install --save-dev webpack
npm install --save-dev webpack-cli
Update your custom build command to include the path to your webpack.config.js file.
"scripts": {
"build": "webpack --config <path_to_webpack_config>/webpack.config.js",
"start-hot": "node server.js",
"start": "webpack-dev-server --content-base public/ --inline --port 3001"
},
Try running your build script again:
npm run build
Hopefully that helps!
I have built a basic babel webpack starter project that i clone from git. The git repository has no dest/output/build folder nor does it have any initial build files. I am knew to webpack so i assumed that when the webpack-dev-server started up it would create a folder (in my case 'build') and do a first time compilation of the source files. Instead i am forced to start the process manually only then after the initial compilation will webpack-dev-server compile the changes.
What am i missing ?
package.json
{
"name": "babel-webpack-starter",
"version": "1.0.0",
"description": "A Babel Webpack Stater Pack for compiling ES2015/ES6, ES2016/ES7 and ES2017 code doen to ES5. This will allow for the use of ES6 modules and later ECMAScript features such as async/await. Also includes a webpack-dev-server to auto load server without having to re-compile.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode=development",
"start": "webpack-dev-server --output-public-path=/build/ --mode=development"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"webpack": "^4.16.4",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}
}
webpack.config.js
// Load Node modules
const path = require('path');
// Export modules
module.exports = {
entry: {
app: './src/app.js'
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'app.bundle.js'
},
module: {
rules: [{
test: /\.js?$/,
exclude: '/node_modules',
loader: 'babel-loader',
query: {
presets: ['env']
}
}]
}
};
webpack-dev-server will not put your app.bundle.js in your working directory, it will compile your files in-memory.
If you want to access the files, add public path to the output option or which you have done there in the npm start script.
output: {
...
publicPath: '/build/'
}
Now, your files will be available in localhost:<port>/build/app.bundle.js
You can check this site out for more information
Desired Behaviour
I want to be able to:
Use webpack to define a build process that uses babel to convert an ES6 server side Node file to "plain javascript"
Current Behaviour
If I just run:
node app.js
I get import errors:
import express from "express";
^^^^^^^
SyntaxError: Unexpected identifier
What I've Tried
When I try and define a build process in webpack, I get errors like:
Can't resolve
tls/net/fs/dns/child_process/aws-sdk/./local_settings/npm/node-gyp
There is a possible solution offered here, but it doesn't resolve all errors (these errors remain: aws-sdk, ./local_settings, npm, node-gyp):
target: "node"
There are also warnings like:
Module parse failed: Unexpected token
Critical dependency: the request of a dependency is an expression
This "how do I use ES6 in production?" question seems to be common, eg:
NodeJS in ES6/ES7, how do you do it in production?
Quickstart guide to using ES6 with Babel, Node and IntelliJ
Getting ready for production use
Is it okay to use babel-node in production
but none of the answers I have found seem definitive or specifically relate to a webpack solution.
Below is code I have now:
from webpack.config.js
const path = require('path');
console.log("the __dirname is: " + __dirname);
module.exports = {
entry: "./src/js/app.js",
output: {
filename: "app.js",
path: path.resolve(__dirname, "dist/js")
},
target: "node",
mode: "development",
module: {
rules: [{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
options: {
presets: ["env", "stage-0"]
}
}
},
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
},
{
test: /\.less$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" },
{ loader: "less-loader" }
]
},
{
test: /\.jpg$/,
use: [
{ loader: "url-loader" }
]
}
]
}
}
from package.json:
...
"main": "app.js",
"scripts": {
"start": "nodemon ./app.js --exec babel-node -e js",
"build": "webpack",
"watch": "webpack --w"
},...
"dependencies": {
"bcrypt": "^2.0.1",
"body-parser": "^1.18.2",
"cors": "^2.8.4",
"express": "^4.16.3",
"jsonwebtoken": "^8.2.1",
"mongodb": "^3.0.8"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.11",
"file-loader": "^1.1.11",
"less": "^3.0.4",
"less-loader": "^4.1.0",
"style-loader": "^0.21.0",
"url-loader": "^1.0.1",
"webpack": "^4.8.3",
"webpack-cli": "^2.1.3"
}
Question
What should my webpack.config.js and package.json files look like in order to successfully convert the ES6 file to "plain javascript"?
By default, Webpack tries to bundle everything into a single .js file. For client-side projects this is fine, but for NodeJS projects it becomes slightly more complicated because you are including code from node_modules as well. Sometimes that can cause errors like the one you're seeing here.
What you want to do in addition to targets: "node" is tell Webpack not to bundle external dependencies (i.e. node_modules).
There's a useful library called webpack-node-externals that helps with this:
var nodeExternals = require('webpack-node-externals');
...
module.exports = {
...
target: 'node', // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
...
};
So it's not really about "plain javascript", more like trying to get Webpack to output a file which is compatible with the NodeJS ecosystem.