Webpack error - can't resolve loader in 'tmp/build...' - node.js

I recently added the eslint and eslint-loader NPM modules as dev dependencies to my React app (with Webpack) - all is well when I am running my development server locally. However, when I try to build the production release locally, it fails with the following error:
npm ERR! Darwin 16.7.0
npm ERR! argv "/Users/user/.nvm/versions/node/v6.11.1/bin/node" "/Users/user/.nvm/versions/node/v6.11.1/bin/npm" "run" "build:client"
npm ERR! node v6.11.1
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! App#0.0.1 build:client: `webpack`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the App#0.0.1 build:client script 'webpack'.
On a whim, I also tried pushing to Heroku, where I got a more helpful error:
ERROR in Entry module not found: Error: Can't resolve 'eslint-loader' in '/tmp/build_5f502b6d28fee058cbe484b873b6e7cb'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! App#0.0.1 build:client: `webpack`
npm ERR! Exit status 2
So this is the part where I admit that I am a bit baffled by Webpack, and would greatly appreciate some guidance and clarification, if that can be offered. I've seen others with a similar issue, but never referring to the '/tmp/build...' directory.
So first off, here are the scripts and some other relevant bits in my package.json:
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.bundle.js",
"start:dev": "concurrently --prefix \"[{name}]\" -k \"webpack -d --watch --colors\" \"nodemon index.js\" -n \"WEBPACK,NODEMON\" --content-base public/",
"build:client": "webpack",
"build:server": "webpack --config webpack.server.config.js",
"build": "NODE_ENV=production npm run build:client && npm run build:server",
"postinstall": "npm run build"
},
"dependencies": {
"webpack": "^2.6.0"
},
"devDependencies": {
"eslint": "^4.4.1",
"eslint-loader": "^1.9.0",
"eslint-plugin-babel": "^4.1.2",
"eslint-plugin-react": "^7.1.0"
"webpack-dev-server": "^2.4.5"
}
As the errors say, build:client is where this is failing...
And from webpack.config.js:
"module": {
"loaders": [
{
"exclude": /node_modules/,
"loader": "babel-loader",
"query": { "presets": ["react", "es2015", "stage-2"] },
"test": /\.jsx?$/
},
{
"exclude": /node_modules/,
"loader": "babel-loader",
"test": /\.js$/
},
{
"enforce": "pre",
"exclude": /node_modules/,
"loader": "eslint-loader",
"query": { "presets": ["react", "es2015", "stage-2"] },
"test": /\.jsx$/
},
{
"enforce": "pre",
"exclude": /node_modules/,
"loader": "eslint-loader",
"test": /\.js$/
}
]
}
If I comment out the sections referring to eslint-loader in the config, then it builds just fine, so as Heroku so keenly observed, the problem must be with those lines. Can anyone suggest a fix for this problem? I'm not sure that I really understand what is going wrong, let alone how to resolve it.
Edit Here is my complete webpack.config.js:
const webpack = require("webpack")
module.exports = {
"entry": "./src/index.jsx",
"module": {
"loaders": [
{
"exclude": /node_modules/,
"loader": "babel-loader",
"query": { "presets": ["react", "es2015", "stage-2"] },
"test": /\.jsx?$/
},
{
"exclude": /node_modules/,
"loader": "babel-loader",
"test": /\.js$/
},
{
"enforce": "pre",
"exclude": /node_modules/,
"loader": "eslint-loader",
"query": { "presets": ["react", "es2015", "stage-2"] },
"test": /\.jsx$/
},
{
"enforce": "pre",
"exclude": /node_modules/,
"loader": "eslint-loader",
"test": /\.js$/
}
]
},
"output": {
"filename": "./public/bundle.js",
"publicPath": "/"
},
"plugins": [
new webpack.DefinePlugin({
"API_URL": JSON.stringify(process.env.API_URL || "http://localhost:4000/api/v1"),
"IS_STAGING": JSON.stringify(process.env.IS_STAGING || "false"),
"NODE_ENV": JSON.stringify(process.env.NODE_ENV || "development")
})
]
}

Thanks to #Marek and #archae0pteryx for their thoughts on this - I came up with a fix, though it may not be optimal...
Basically, I created a webpack.dev.config.js file that included the references to eslint-loader... which is just fine, since I only want to use the linting in local development. Then I removed those references from my main config file updated package.json to use the dev config in local development, and it was all good.

I had the same problem, but for me, that wasn't solved in such way.
I solved it using this answer by EvHaus
https://github.com/babel/babel-loader/issues/166#issuecomment-184763126
This is the solution:
plugins: [
require.resolve("babel-plugin-add-module-exports"),
require.resolve("babel-plugin-transform-decorators-legacy")
],
presets: [
require.resolve("babel-preset-es2015"),
require.resolve("babel-preset-stage-0"),
require.resolve("babel-preset-react")
]

I think your problem may be that you have EsLint in devDependencies. If I remember correctly Heroku installs only dependencies of your app. Try moving EsLint into dependencies.

Related

Jest. Inaccessible host: `localhost'. This service may not be available in the `local-env' region

I write service on nodejs and serverless technology. I need to test the service with integration tests. As testing and running library I have chosen Jest. I specified dynamodb configuration to test it as well.
When I try to run tests on jest in my local machine, tests completed successfully without any error. But when tests run in bitbucket pipelines it comes with an error:
UnknownEndpoint: Jest: Got error running globalSetup - /opt/atlassian/pipelines/agent/build/node_modules/#shelf/jest-dynamodb/setup.js, reason: Inaccessible host: `localhost'. This service may not be available in the `local-env' region. at Request.ENOTFOUND_ERROR (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/event_listeners.js:530:46) at Request.callListeners (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/sequential_executor.js:106:20) at Request.emit (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/sequential_executor.js:78:10) at Request.emit (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/request.js:688:14) at error (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/event_listeners.js:362:22) at ClientRequest.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/http/node.js:99:9) at ClientRequest.emit (events.js:376:20) at ClientRequest.emit (domain.js:470:12) at Socket.socketErrorListener (_http_client.js:475:9) at Socket.emit (events.js:376:20) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! notification-service#1.0.0 test: `jest --config ./jest.config.json --runInBand` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the notification-service#1.0.0 test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2021-06-21T16_19_24_134Z-debug.log
This is my pipelines:
pipelines:
default:
- step:
name: npm install and test
script:
- apk add openjdk8
- npm install
- sls dynamodb install
- npm run test
services:
- docker
caches:
- node
definitions:
services:
docker:
memory: 1024
jest.config.json:
{
"testRegex": "((\\.|/*.)(spec))\\.js?$",
"preset": "#shelf/jest-dynamodb",
"setupFiles": [
"./jest.init.js"
]
}
jest.init.js:
import 'babel-polyfill';
jest.dynamodb-config.js:
module.exports = {
port: "8001",
tables: [
{
"TableName": "templates",
"BillingMode": "PAY_PER_REQUEST",
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "created_at",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "created_at",
"KeyType": "RANGE"
}
]
},
{
"TableName": "types",
"BillingMode": "PAY_PER_REQUEST",
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "created_at",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "created_at",
"KeyType": "RANGE"
}
]
}
],
};
package.json:
{
"name": "notification-service",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --config ./jest.config.json --runInBand",
"test:watch": "npm run test -- --watch",
"doc": "jsdoc -r src/ -d doc -p --readme readme.md",
"deploy_locally": "serverless offline --stage test",
"deploy_party": "serverless deploy --stage party --region ${region:-eu-central-1} --aws-profile ${profile:-sandbox}",
"deploy_cloud": "serverless deploy --stage cloud --region ${region:-eu-central-1} --aws-profile ${profile:-cloud}",
"start": "sls offline start --stage test"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/preset-env": "^7.14.5",
"#jest/globals": "^27.0.3",
"#shelf/jest-dynamodb": "^1.8.1",
"aws-sdk": "^2.930.0",
"aws-sdk-mock": "^5.2.1",
"aws-testing-library": "^1.1.1",
"axios": "^0.21.1",
"babel-jest": "^27.0.2",
"babel-polyfill": "^6.26.0",
"jest": "^27.0.4",
"jsdoc": "^3.6.7",
"serverless": "^2.46.0",
"serverless-dotenv-plugin": "^3.9.0",
"serverless-dynamodb-local": "^0.2.39",
"serverless-offline": "^7.0.0",
"serverless-offline-sqs": "^4.1.1",
"serverless-pseudo-parameters": "^2.5.0",
"serverless-webpack": "^5.5.1",
"webpack": "^5.39.0"
},
"dependencies": {
"messente_api": "^1.4.0",
"moment": "^2.29.1",
"uuid": "^8.3.2"
},
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
},
"setupFilesAfterEnv": [
"./node_modules/aws-testing-library/lib/jest/index.js"
]
}
}
How can I fix it?

npm no such file or directory when trying to run 'npm run deploy'

I get this error:
no such file or directory, stat 'C:\Users\nessa\VS Code\React-Django\website\frontend\build'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! frontend#1.0.0 deploy: `gh-pages -d build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the frontend#1.0.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
when trying to run npm run deploy.
This is my package.json file located in my .\website\frontend
{
"name": "frontend",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.13.13",
"#babel/preset-env": "^7.13.12",
"#babel/preset-react": "^7.13.13",
"babel-loader": "^8.2.2",
"css-loader": "^5.2.0",
"gh-pages": "^3.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"style-loader": "^2.0.0",
"webpack": "^5.28.0",
"webpack-cli": "^4.6.0"
},
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.13.0",
"#material-ui/core": "^4.11.3",
"#material-ui/icons": "^4.11.2",
"#material-ui/lab": "^4.0.0-alpha.57",
"react-material-ui-carousel": "^2.2.4",
"react-router-dom": "^5.2.0",
"react-speech": "^1.0.2"
}
}
I've been running npm run dev from the same location all the time without issues. I'm trying to host my react app on Github. I tried reinstalling gh-pages in case a file didn't properly install but am having the same problems. What should I do to resolve this?
Edit: I tried to do what was said here, but still didn't work. Here's my webpack.config.js if that helps.
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, './static/frontend'),
filename: '[name].js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
optimization: {
minimize: true,
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
}),
],
}
I also changed "build": "webpack --mode production" in my package.json scripts to "build": "webpack --mode development", in order to run npm run build, but the problem still isn't fixed.
This was my error:
ENOENT: no such file or directory, stat 'C:\Users...\dist'
So I added
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
on package.json in "scripts".
And run the command npm install gh-pages --save-dev in terminal.
Finishing with npm run deploy.
After you add predeploy, deploy in script in JSON file.
run : npm run build
run : npm run deploy

SyntaxError: unexpected token ':' (colon) typescript webpack

has anyone else had this issue?
(node:24328) ExperimentalWarning: The ESM module loader is experimental.
file:///Users/xxx19/Documents/projects/planning-poker/src/server.ts:13
wss.on('connection', (ws: WebSocket) => {
^
SyntaxError: Unexpected token ':'
at Loader.moduleStrategy (internal/modules/esm/translators.js:84:18)
at async link (internal/modules/esm/module_job.js:36:21)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! game-library#1.0.0 server: `tsc && node ./src/server.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the game-library#1.0.0 server script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Here is my package.json.
I am running npm run server which is "tsc && node ./src/server.ts".
Running npm run tsc does not show any errors.
{
"name": "game-library",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
"client": "webpack-dev-server --inline --config webpack.config.cjs",
"server": "tsc && node ./src/server.ts",
"build": "webpack --config webpack.config.cjs",
"tsc": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.3",
"express": "^4.17.1",
"prettier": "^2.0.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"ts-node": "^8.10.1",
"ws": "^7.2.5"
},
"devDependencies": {
"#babel/plugin-transform-typescript": "^7.9.6",
"#types/express": "^4.17.6",
"#types/node": "^13.13.2",
"#types/react": "^16.9.34",
"#types/react-dom": "^16.9.6",
"#types/ws": "^7.2.4",
"#typescript-eslint/eslint-plugin": "^2.29.0",
"#typescript-eslint/parser": "^2.29.0",
"eslint": "^6.8.0",
"eslint-plugin-react": "^7.19.0",
"source-map-loader": "^0.2.4",
"ts-loader": "^7.0.1",
"typescript": "^3.8.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
}
}
webpack.config:
'use-strict';
module.exports = {
// Enable sourcemaps for debugging webpack's output.
entry: './src/index.tsx',
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.tsx', '.js'],
modules: ['src', 'node_modules'],
},
module: {
rules: [
{
test: /\.ts(x?)$/,
exclude: /node_modules/,
include: path.resolve(__dirname, 'src'),
use: [
{
loader: 'ts-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',
},
};
ts.config:
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es6",
"jsx": "react",
},
"resolve": {
"extensions": [".ts", ".tsx", ".js"]
},
}

npm run dev command is not working with the following configurations

I'm following this tutorials for my first TDD project: https://medium.com/developer-circles-lusaka/how-to-write-an-express-js-server-using-test-driven-development-921dc55aec07
I have install all the required dependency and my package.json file looks like below.
{
"name": "lms_webapi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "babel src -d dist --source-maps",
"serve": "NODE_ENV=production node dist/index.js",
"start": "NODE_ENV=development babel-node src/index.js",
"dev": "DEBUG=server:debug NODE_ENV=development nodemon src/index.js --exec babel-node",
"test": "DEBUG=server:debug NODE_ENV=test mocha --require babel-core/register --reporter spec --exit tests/ --exec babel-node",
"test:watch": "DEBUG=server:debug NODE_ENV=development mocha --require babel-core/register --watch --reporter spec tests/ --exec babel-node",
"eslint": "eslint src/**/*.js --ignore-pattern \"node_modules/\""
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-cli": "^6.26.0",
"babel-preset-node8": "^1.2.0",
"chai": "^4.2.0",
"config": "^3.3.1",
"debug": "^4.1.1",
"express": "^4.17.1",
"mocha": "^7.1.1",
"mongoose": "^5.9.7",
"supertest": "^4.0.2"
},
"babel": {
"presets": [
"node8"
]
},
"engines": {
"node": ">=8"
},
"eslintConfig": {
"plugins": [
"node"
],
"extends": [
"eslint:recommended",
"plugin:node/recommended"
],
"rules": {
"node/no-unsupported-features/es-syntax": 0,
"node/no-unsupported-features/es-builtins": 0
},
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2018
}
}
}
after running the command npm run dev from terminal, I got the following error.
lms_webapi#1.0.0 dev E:\Test\LMS_WebAPI
DEBUG=server:debug NODE_ENV=development nodemon src/index.js --exec babel-node
Der command "DEBUG" is either false or not found.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! lms_webapi#1.0.0 dev: DEBUG=server:debug NODE_ENV=development nodemon src/index.js --exec babel-node
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the lms_webapi#1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Your help will be highly appreciated.
Thank you!
The dev-script fails because it tries to look for the DEBUG command instead of setting the environment variable. This tells us that you're probably on a windows machine. Hence, you need can adjust setting the env-variables as follows:
...
"scripts": {
...
"dev": "set DEBUG=server:debug && set NODE_ENV=development && nodemon src/index.js --exec babel-node",
},
...

Why can't I run Webpack with React?

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

Resources