Webpack Breaks Mocha in Node.js App - node.js

I have a node.js app that uses Mocha and Expect for testing. All the tests work great, until I installed webpack for react. Now when I run "npm test" I get the following error:
Error: Cannot find module 'should'
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> (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\watchpack\test\DirectoryWatcher.test.js:2: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)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:230:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:227:14)
at Mocha.run (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:495:10)
at Object.<anonymous> (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\bin\_mocha:469:18)
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)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
npm ERR! Test failed. See above for more details.
Below is my package.json:
{
"name": "cool-map",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node server.js",
"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha **/*.test.js",
"test-watch": "nodemon --exec \"npm test\""
},
"engines": {
"node": "6.2.2"
},
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.3.0",
"body-parser": "^1.15.2",
"express": "^4.14.0",
"jsonwebtoken": "^7.1.9",
"lodash": "^4.15.0",
"mongodb": "^2.2.5",
"mongoose": "^4.5.9",
"validator": "^5.6.0",
"hbs": "^4.0.0",
"babel-preset-stage-0": "^6.24.1",
"express": "^4.15.2",
"react": "^0.14.7",
"react-dom": "^0.14.7"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.0.2",
"nodemon": "^1.10.2",
"supertest": "^2.0.0",
"babel-core": "^6.5.1",
"babel-loader": "^6.2.2",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"webpack": "^1.12.13"
}
}
I'm not sure if webpack.config is necessary to post, but just in case:
module.exports = {
entry: './app/app.jsx',
output: {
path: __dirname,
filename: './public/bundle.js'
},
resolve: {
root: __dirname,
alias: {
AdminUserTable: 'app/components/AdminUserTable.jsx'
},
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/
}
]
}
};
Running the command npm test works great when I uninstall webpack, I'm not sure why It's breaking mocha.

Your package.json is missing should as a dependency.
Install it via;
npm install --save-dev should
Also I would recommend you look into chai which in my opinion provides a slightly different API.

should is an expressive, readable, framework-agnostic assertion library. The main goals of this library are to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.
By default (when you require('should')) should extends the Object.prototype with a single non-enumerable getter that allows you to express how that object should behave. It also returns itself when required with require.
It is also possible to use should.js without getter (it will not even try to extend Object.prototype), just require('should/as-function'). Or if you already use version that auto add getter, you can call .noConflict function.
Results of (something).should getter and should(something) in most situations are the same
Better u install node dependency should with npm as below
npm install --save should
should-reference

Related

npm not start. Error: Cannot find module 'webpack-cli/bin/config-yargs'

when I give npm start comand its show
internal/modules/cjs/loader.js:883
throw err:
Error: Cannot finde module'webpack-cli/bin/config-yargs'
plz help me to solve this problem
use os : windows 10
node version is v14.16.1
> chapter15.1#1.0.0 start
> webpack-dev-server
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'webpack-cli/bin/config-yargs'
Require stack:
- C:\Users\user\Desktop\JS full Course\chapter15.1\node_modules\webpack-dev-server\bin\webpack-dev-server.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\user\Desktop\JS full Course\chapter15.1\node_modules\webpack-dev-server\bin\webpack-dev-server.js:65:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\user\\Desktop\\JS full Course\\chapter15.1\\node_modules\\webpack-dev-server\\bin\\webpack-dev-server.js'
]
}
webpack version 5.37.1
webpack-cli version 4.7.0
webpack-dev-server version 3.11.2
Package.json file is
{
"name": "chapter15.1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/cli": "^7.14.3",
"#babel/core": "^7.14.3",
"#babel/node": "^7.14.2",
"#babel/polyfill": "^7.12.1",
"#babel/preset-env": "^7.14.2",
"#babel/register": "^7.13.16",
"babel-loader": "^8.2.2",
"babel-register": "^6.26.0",
"html-webpack-plugin": "^5.3.1",
"webpack": "^5.37.1",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.11.2"
}
}
In the Package.json file add a serve script like below and remove the start script. See the documentation.
{
"scripts": {
"serve": "webpack serve"
},
Then Run this command in your terminal.
npm run serve

Non-deterministic behavior of npm

After running npm install, I have noticed that sometimes package-lock.json changes.
This section:
"dependencies": {
"websocket": {
"version": "git://github.com/frozeman/WebSocket-Node.git#6c72925e3f8aaaea8dc8450f97627e85263999f2",
"from": "git://github.com/frozeman/WebSocket-Node.git#6c72925e3f8aaaea8dc8450f97627e85263999f2",
"requires": {
"debug": "^2.2.0",
"nan": "^2.3.3",
"typedarray-to-buffer": "^3.1.2",
"yaeti": "^0.0.6"
}
}
}
}
Changes to this:
"dependencies": {
"websocket": {
"version": "git://github.com/frozeman/WebSocket-Node.git#6c72925e3f8aaaea8dc8450f97627e85263999f2",
"from": "git://github.com/frozeman/WebSocket-Node.git#6c72925e3f8aaaea8dc8450f97627e85263999f2",
"requires": {
"debug": "^2.2.0",
"nan": "^2.3.3"
}
}
}
}
And when that happens, I get a runtime error:
module.js:538
throw err;
^
Error: Cannot find module 'typedarray-to-buffer'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\...\node_modules\web3-providers-ws\node_modules\websocket\lib\W3CWebSocket.js:18:16)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
After some debugging, this error is for require("web3") in my code.
Here is the relevant section in my package.json file:
"devDependencies": {
"chai": "^4.1.2",
"chai-as-promised": "^7.0.0",
"bignumber.js": "^7.0.0",
"decimal.js": "^10.0.0",
"web3": "1.0.0-beta.34",
"ethereumjs-testrpc-sc": "6.1.2",
"ganache-cli": "6.1.0",
"solidity-coverage": "0.4.14",
"truffle": "4.1.3"
}
How can I resolve this, and why does npm work differently from time to time?

Cannot find module 'loadash'

I am getting the following error on executing command: npm start
Cannot find module 'loadash'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (<Path...>/data/dbConnector.js:3:1)
at Module._compile (module.js:643:30)
at loader (<Path...>\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (<Path...>\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (<Path...>/data/resolvers.js:2:1)
at Module._compile (module.js:643:30)
at loader (<Path...>\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (<Path...>\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
[nodemon] app crashed - waiting for file changes before starting...
package.json file:
{
"name": "essentials",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon ./index.js --exec babel-node -e js"
},
"author": "test",
"license": "ISC",
"dependencies": {
"casual": "^1.5.19",
"express": "^4.16.3",
"express-graphql": "^0.6.12",
"graphql": "^0.13.2",
"graphql-tools": "^2.23.1",
"loadash": "^1.0.0",
"mongoose": "^5.0.12",
"nodemon": "^1.17.2",
"sequelize": "^4.37.5",
"sqlite3": "^4.0.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-0": "^6.24.1"
}
}
I followed the below links but still none of them are working for me.
https://stackoverflow.com/questions/27431187/cannot-find-module-lodash
https://stackoverflow.com/questions/27431187/cannot-find-module-lodash/36618374
Also I see the below warning message:
Problems loading reference 'https://schemastore.azurewebsites.net/schemas/json/package.json': Unable to load schema from 'https://schemastore.azurewebsites.net/schemas/json/package.json': Unable to connect to https://schemastore.azurewebsites.net/schemas/json/package.json. Error: self signed certificate in certificate chain
Can anyone help me to fix this issue?
The package name should be lodash, not loadash.
After you correct that in packages.json you need to execute npm install.

node.js app azure deployment from bitbucket

I am getting following error when i try to deploy nodejs app from bitbucket.
Command: "D:\home\site\deployments\tools\deploy.cmd"
Handling node.js deployment.
KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
undefined:38
}
^
An error has occurred during web site deployment.
SyntaxError: Unexpected token } in JSON at position 1090
at Object.parse (native)
at Object.<anonymous> (D:\Program Files (x86)\SiteExtensions\Kudu\63.60712.2926\bin\Scripts\selectNodeVersion.js:179:44)
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)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
undefined:38\r\n}\r\n^\r\n\r\nSyntaxError: Unexpected token } in JSON at position 1090\r\n at Object.parse (native)\r\n at Object.<anonymous> (D:\Program Files (x86)\SiteExtensions\Kudu\63.60712.2926\bin\Scripts\selectNodeVersion.js:179:44)\r\n at Module._compile (module.js:570:32)\r\n at Object.Module._extensions..js (module.js:579:10)\r\n at Module.load (module.js:487:32)\r\n at tryModuleLoad (module.js:446:12)\r\n at Function.Module._load (module.js:438:3)\r\n at Module.runMain (module.js:604:10)\r\n at run (bootstrap_node.js:394:7)\r\n at startup (bootstrap_node.js:149:9)\r\nD:\Program Files (x86)\SiteExtensions\Kudu\63.60712.2926\bin\Scripts\starter.cmd "D:\home\site\deployments\tools\deploy.cmd"
Using node 6.9.1
package.json
{
"name": "example",
"version": "1.0.0",
"description": "REST APIs for example",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+abc"
},
"author": "abc",
"license": "ISC",
"homepage": "abc#readme",
"dependencies": {
"azure-storage": "^2.1.0",
"body-parser": "^1.17.1",
"change-case": "^3.0.1",
"dateformat": "^2.0.0",
"dotenv": "^4.0.0",
"express": "^4.15.2",
"jsonwebtoken": "^7.3.0",
"minimist": "^1.2.0",
"morgan": "^1.8.1",
"mssql": "^4.0.1",
"multer": "^1.3.0",
"nconf": "^0.8.4",
"node-friendly-response": "^3.1.4",
"request": "^2.81.0",
"require-dir": "^0.3.1",
"swagger-node-express": "^2.1.3",
"tedious": "^2.0.0",
"tedious-promises": "^0.4.1",
"underscore": "^1.8.3",
"winston": "^2.3.1"
}
}
Is it because of the node modules which i have used?
Or is it the problem during the loading of modules?
It seems that your package.json is not a valid JSON.
Update:
Kudu uses the following code to read the package.json file, see line 179 of selectNodeVersion.js.
json = existsSync(packageJson) && JSON.parse(fs.readFileSync(packageJson, 'utf8'));
So, before you push the package.json to github, you can use the code below to verify the file on your local:
var fs = require("fs");
var json = JSON.parse(fs.readFileSync("./package.json", 'utf8'));
console.log(json);
For example, I added one more } at line 38 in package.json your provided above, I would get the same error as yours:
Or you can just use the command npm install to check it:

Error: Cannot find module 'lodash/keys' - Heroku Node environment

I keep getting this error when deploying my Node and Mean app to Heroku. The app seems to work normally when I run it locally but does not like it when I push it to Heroku.
module.js:471
throw err;
^
Error: Cannot find module 'lodash/keys'
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> (/app/node_modules/mongoose/node_modules/async/internal/iterator.js:16:13)
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)
This is my package.json file
{
"name": "utdknow",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.6.12",
"body-parser": "^1.14.1",
"crypto-js": "^3.1.8",
"ejs": "^2.3.4",
"express": "^4.13.3",
"lodash": "^4.17.3",
"lodash.keys": "^4.2.0",
"moment": "^2.17.0",
"mongoose": "^4.2.5",
"ng-file-upload": "^12.0.4"
}
}
It seems like modules are not correctly installed. Try cleaning cache and deploy your app to re-install all dependencies:
https://github.com/heroku/heroku-repo
heroku plugins:install heroku-repo
heroku repo:purge_cache --app appname

Resources