How to use a npm modules in nightwatchjs based project - node.js

Planning a nightwatchjs based project, for data driven web testing, after loading list of options from a data.json file to iterate through collections etc. I want to include underscore or lodash npm modules. I don't see a package.json where i can include dependencies to generate node_modules through npm install option.
How can one use npm modules with nightwatchjs project ?

Haven't used nightwatch, but have used mocha, and if you just create a package.json file in the directory where you're running your tests, then do an npm install, in mocha you can then simply use those packages. I would assume that since nightwatch is js, it should be the same. Just try it out.

you may create your own using package.json specification in your project folder,
there is mine for docker:
{
"name": "e2e-tests",
"version": "0.0.1",
"main": "index.js",
"scripts": { },
"author": "Alex K",
"license": "MIT",
"dependencies": {
"chromedriver": "^2.37.0"
"i18n": "*",
"nightwatch": "0.9.21",
"nightwatch-html-reporter": "*"
}

Related

How to use npm-shrinkwrap with workspaces?

I have a monorepo project, with one of the packages being a cli. And to prevent npm supply chain attack I am thinking of using npm-shrinkwrap.
The documentation here says:
The recommended use-case for npm-shrinkwrap.json is applications deployed through the publishing process on the registry: for example, daemons and command-line tools intended as global installs or devDependencies
This is exactly what i want as I am deploying a command-line tools. The only problem is, it seems npm-shrinkwrap does not support workspaces.
Let's say the cli part of my monorepo can be found in the path: /code/packages/cli, when I switch to it and run npm shrinkwrap I get the error:
npm ERR! code ENOWORKSPACES
npm ERR! This command does not support workspaces.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/me/.npm/_logs/2022-05-16T20_53_41_147Z-debug-0.log
If I switch to the root of the project, that is /code/ and I run npm shrinkwrap, it does generates a npm-shrinkwrap.json with the following structure:
{
"name": "root",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "root",
"version": "1.0.0",
"license": "ISC",
"workspaces": [
"packages/*"
],
"devDependencies": {
"lerna": "^4.0.0"
}
},
"packages/cli": {
"name": "#proj/cli",
"version": "1.0.0",
"extraneous": true,
"license": "ISC"
}
}
}
But the only problem is, this is not the exact structure that should be created for the cli. Also when I publish the package (using lerna), the npm-shrinkwrap.json is never included in the cli package.
Any tips on how to make npm-shrinkwrap work with workspaces?
Basically how to have the npm-shrinkwrap.json generated for a package in a monorepo and have that inlcuded when that package is released?
The tool npm-lockfile uses npm's internals and works inside of monorepos.
Unfortunately, they're using an old version of #npm/arborist, so it doesn't quite work properly, and you can use my patch for now

Managing Node JS dependencies

I am learning node js and I have put together a small website which is using a few dependencies like jquery and a couple of other js files. When it comes to moving the site to a live server how do I migrate the dependencies that I used in production? Would I have to download the jquery files and other js library files then add the link into my script tags like you normally would as I guess everything in production points at the node_modules folder?
Sorry for sounding a little daft but I’m still learning my way with npm.
Appreciate any advice
Rufus
In Node.js, server side dependencies are saved in the package.json file and these describe any dependencies you'll need.
You create this file by typing
npm init
This will prompt you for some details (which you can skip with -y flag.)
To install your server side dependencies, I presume you've used:
npm install <dependency>
To ensure this is saved you can type
npm install <dependency> --save
e.g.
npm install lodash --save
This will update your package.json.
package.json should be stored in source control and your dependencies can be restored using:
npm install.
An example of a package.json file would look like this:
{
"name": "node_test_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.11"
}
}
If you see a package-lock.json file (later versions of NPM), this should be added to source control too.
Personally I just use npm install lodash
Then
app.use('/scripts/lodash', express.static(__dirname + '/node_modules/lodash/'));
that way I can use this as (in node.js)
const _ = require('lodash');
or in an html file as
<script src='/scripts/lodash/lodash.js'></script>
Preventing the whole "copying to public/js"...
Or if you're just talking about a build process, you just run npm install on your productions server and it installs all the packages from packages.json again.

How to ignore scoped packages' node_modules/ directory during npm install?

I have a repository containing a package.json which contains scoped dependencies. I also have an .npmignore file intended to whitelist all files and subdirectories in dist/. The problem is all of the scoped dependencies are included when running npm install #private/a another repository. This includes both private npm packages and public packages such as #uirouter.
package.json:
{
"name": "#private/a",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git#bitbucket.org/private/a.git"
},
"author": "",
"license": "ISC",
"homepage": "https://bitbucket.org/private/a#readme",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-angular-embed-templates": "^2.3.0",
"gulp-concat": "^2.6.1",
"gulp-jshint": "^2.0.4",
"gulp-rename": "^1.2.2",
"gulp-sass": "^3.0.0",
"gulp-uglify": "^2.0.0",
"jshint": "^2.9.4"
},
"dependencies": {
"#private/b": "^1.0.0",
"#private/c": "^1.0.0"
}
}
.npmignore
**
!dist/**
Despite these two files when I run npm install #private/a --save within another repository it is installing the dependency along with all it's scoped dependencies:
/node_modules/#private/a/dist/index.js
/node_modules/dist/css/styles.css
/node_modules/#private/a/node_modules/#private/b
/node_modules/#private/a/node_modules/#private/c
package.json
It should only be this:
/node_modules/#private/a/dist/index.js
/node_modules/dist/css/styles.css
package.json
How can I achieve this? I have tried different variations of the .npmignore but have not had any luck.
.npmignore is irrelevant to what you are trying to do. This file only decides which parts of your npm package code ends up in npm registry. So it is working as advertised.
Your problem must be in your npmconfig or because of using an older version of npm. The latest version installs stuff as so:
/node_modules/#private/a/dist/index.js
/node_modules/#private/b/...
/node_modules/#private/c/...
package.json
I have verified that this is happening with latest npm. But there used to be a time when npm installed dependencies into a nested structure. See this for example. So I suggest:
Making sure you have latest node and npm.
Making sure your npm config is not forcing legacy bundling. Run npm get legacy-bundling. Make sure this is false.
There are few cases where the nesting of dependencies happens legitimately even with the latest npm. See this. But I am guessing your problem is not due to this. You can test by simply doing npm install #private/a in an empty folder.
Node will install your package files along with all the dependencies declared under dependencies field.
How the dependencies tree is build, depends on which version of npm do you use.
If your package doesn't need those dependencies to run, it means they are just dev dependencies and you can safely list them under devDependencies field.
Dev dependencies are only installed when you run an npm install inside the plugin directory.
You need to lock your dependency. You might want to check out npm shrinkwrap.

npm install does not install the dependencies of individual node modules into the respective node modules folders

npm install was installing node modules correctly until recently when I ran into a bug. Now, npm install does not install the dependencies of individual node modules into the respective node modules folders.
See screenshot for what I mean.
The finder window at the forefront shows the correct npm install before the bug. npm install express would download and put the files in correct folders. The accepts folder is a node module of express and has its own node modules, mime types and negotiator.
Now, the accepts folder and its own node modules sit out at the same level as express folder. As seen in the finder window in the back.
This is causing me not to be able to upload to heroku.
Please advise on how to fix.
Here's my package.json
{
"name": "node-muse-examples-webgui",
"version": "0.1.0",
"description": "An example on how to use the node-muse module in a web interface.",
"main": "index.js",
"engines": {
"node": "7.2.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/ShaPOC/node-muse/tree/master/examples/webgui"
},
"author": "Jimmy Aupperlee <j.aup.gt#gmail.com>",
"license": "GPLv3",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"leapjs": "^0.6.4",
"mongodb": "^2.2.7",
"node-muse": "^0.1.0",
"socket.io": "^1.3.5"
}
}
That behavior is not a bug, it actually is a new behavior introduced with npm#3.
That normally should not cause any conflict or problems, but if it does in your case try to install it with
npm install --legacy-bundling
instead, so delete the whole node_modules folder and reinstall it with that command.

package.json -- How to add already installed packages in 'dependencies' and 'devDependencies' in package.json file after delete package.json?

I have added many packages in my Project using npm.
But after while cleaning my Project i was deleted my package.json by mistake.
so for create new package.json file i have run command nmp init.
now i got the package.json but in my package.json file is like below.
{
"name": "name-will-be-here",
"version": "0.0.1",
"description": "description-will-be-here",
"main": "gulpfile.js",
" dependencies": {},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is there any commands for fill the details like before it is...?
but my Question is that...
How can i get all details of dependencies and devDependencies which was already there before delete?
Short answer is you cant.
Longer answer is that you need to understand how npm 3 works by default and that is to install all dependencies and shared sub dependencies in same top folder so your node_modules holds a lot more than your main dependencies only (unless you used legacy option or older npm).
In future use scm like git or ide history features to revert accidental deletions.

Resources