Custom local npm module as dependency - json

I created a project using npm scripts in order to avoid the use of gulp. The thing is, my project has two scripts:
prepare.sh (uses wget to download some files and do mkdirs)
process.js (transform a json file into another overriding some keys)
package.json
{
"scripts": {
"process": "./process.js",
"prepare": "./prepare.sh $npm_package_config_source $npm_config_env",
"config": "npm run prepare && npm run process"
},
"config": {
"source": "https://myurl"
},
"devDependencies": {
"fs": "0.0.1-security",
"json-override": "^0.2.0"
}
}
So, if I want to apply the transform in this project I run npm run config, but I want this project to be part of another as a local module of a front-end project.
How can I set up my project? And when I add it as a dependency of my front project, how can I call the config script from the package.json of the front project?

You can add a bin object to your package.json which will result in files installed into the node_modules/.bin folder docs.npmjs.com/files/package.json#bin
example
{
"bin": {
"process": "./process.js",
"prepare": "./prepare.sh"
},
"scripts": {
"config": "prepare && process"
},
"devDependencies": {
"fs": "0.0.1-security",
"json-override": "^0.2.0"
}
}
Also since npm runs scripts with node_modules/.bin as part of the path you can simply call them by name only. Just remember to add #!/usr/bin/env node to the top of process.js

Related

JetBrains WebStorm Jest encountered an unexpected token

I have a problem with running a simple project in WebStorm IDE. This is what I get when I hit run:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
/home/patryk/WebstormProjects/Case Converter/node_modules/hs-test-web/hstest/stage/stageTest.js:12
runner = new PureJsApplicationRunner();
^
SyntaxError: Unexpected token =
at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (node_modules/hs-test-web/hstest/index.js:1:110)
At this moment, my project contains just 1 html file. I tried reinstalling nodejs and npm, but that didn't work
Good time of day,
I've encoutered the same problem with one Edu project in WebStorm IDE - when i pressed the "check" button for task - i've got issue:
"test suite failed to run
Jest encountered an unexpected token ..."
How it was solved:
Please check if nodejs and npm installed - At IDE: File\Settings, section: Languages & frameworks > nodejs and npm section > at field: "Node interpreter" you'll find something like : "node /usr/bin/node" and at field: "Package manager" the value "npm /usr/share/npm"
In IDE - you need to expand "Project pane" and to choose view mode "Project Files" - then you need open the file "package.json",
initially this file contains the code:
{
"devDependencies": {
"#types/jest": "^23.3.12",
"hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
"jest": "^27.3.1",
"puppeteer": ">=8.0.0"
},
"scripts": {
"test": "jest"
}
}
You need to add the section for "jest" - please found the complete package.json below
{
"devDependencies": {
"#types/jest": "^23.3.12",
"hs-test-web": "https://github.com/hyperskill/hs-test-web/archive/release.tar.gz",
"jest": "^27.3.1",
"puppeteer": ">=8.0.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"verbose": true,
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"transformIgnorePatterns": ["/node_modules/(?!lodash-es)"],
"testRegex": "test/.*\\.spec\\.ts$"
}
}
So,i hope it help you.

How to build cloud function with different environment like testing/development?

The cloud function should display bucketname on different environment like testing and production
Based on the Firebase a doc I have set config
runtimeconfig.json
{
"project": {
"environment": "testing"
}
}
The config file does not have environment production because I am not sure how the structure should be like for switch from testing to production
index.ts
exports.bucketName = functions.https.onRequest((req, res) => {
const bucketName = functions.config().project.environment;
res.send(bucketName);
});
The bucketname should dynamically changed based on the environment.
Package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"build-and-watch": "tsc -w",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell"
}
}
Can someone suggest whats the best way to switch environment for Cloud Function?
For example a command like npm run build --configuration=test which loads the testing environment likewise for dev npm build --configuration=dev for dev environment.
As #Doug Stevenson suggested set the config based on what environment should do the job. I am using the firebase set and then deploy.

Why Composer's package dependencies are not being installed?

I've got the following composer.json file:
{
"require": {
"local/my_package": "dev-master"
},
"repositories": [
{
"packagist.org": false
},
{
"type": "path",
"url": "my_package/"
}
]
}
which requires my local package which defines the following my_package/composer.json file:
{
"name": "local/my_package",
"require-dev": {
"symfony/console": "*"
}
}
Note: The my_package folder is under Git repository with the default master branch.
Note: To create new repo, run: git init && git add -A && git commit -am'Files'.
When I run: composer install -vvv, the local/my_package package is installed correctly, but nothing else. I would expect that dependencies defined in symfony/console should be installed as well.
Executing command (my_package/): git log -n1 --pretty=%H
- Installing local/my_package (dev-master)
How should I correct my composer.json file, so the package's dependencies can be installed automatically?
It seems the require-dev keyword in the package's composer.json file should be changed to require.
The require-dev property is only required for developing the package (root package).

Gulp doesn't work on Ubuntu 16.04 [duplicate]

As shown in the screen shot below I am not able to run gulp to concat the JavaScript files. Its saying that gulp is not defined.
I have tried the following commands:
npm install -g gulp
npm install gulp
npm install gulp --save-dev
I have also set the environment variables as following:
C:\Users\<user>\AppData\Roaming\npm;C:\Python27;C:\Users\<user>\AppData\Roaming\npm\node_modules;C:\Users\<user>\AppData\Roaming\npm\node_modules\gulp;
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
//script paths
var jsFiles = 'scripts/*.js',
jsDest = 'dist/scripts';
gulp.task('scripts', function() {
return gulp.src(jsFiles)
.pipe(concat('scripts.js'))
.pipe(gulp.dest(jsDest));
});
you just need to install and require gulp locally, you probably only installed it globally
At the command line
cd <project-root> && npm install --save-dev gulp
In your gulpfile.js
var gulp = require('gulp');
this is a different dependency than the command line dependency (that you installed globally). More specifically, it is the same NPM package, but the command line program will execute code usually from a different entry point in the NPM package then what require('X') will return.
If we go to the package.json file in the Gulp project on Github, it will tell the whole story:
{
"name": "gulp",
"description": "The streaming build system",
"version": "3.9.1",
"homepage": "http://gulpjs.com",
"repository": "gulpjs/gulp",
"author": "Fractal <contact#wearefractal.com> (http://wearefractal.com/)",
"tags": [ ],
"files": [
// ...
],
"bin": {
"gulp": "./bin/gulp.js"
},
"man": "gulp.1",
"dependencies": {
// ...
},
"devDependencies": {
// ...
},
"scripts": {
"prepublish": "marked-man --name gulp docs/CLI.md > gulp.1",
"lint": "eslint . && jscs *.js bin/ lib/ test/",
"pretest": "npm run lint",
},
"engines": {
"node": ">= 0.9"
},
"license": "MIT"
}
so at the command line:
$ gulp default
will execute this:
"bin": {
"gulp": "./bin/gulp.js"
},
on the other hand, require('gulp') in your code will return the value of this:
https://github.com/gulpjs/gulp/blob/master/index.js
normally we see this in a package.json file as:
"main": "index.js"
but since this is the default, they just omitted it (which is dumb IMO, better to be explicit, but they aren't the first project I have seen take the lame shorthand route.).
Its occurs on Windows and usually one of the following fixes it:
If you didn't, run npm install gulp on the project folder, even if
you have gulp installed globally.
Normally, It isn't a problem on Windows, but it could be a issue with
the PATH. The package will try to get the PATH from the environment,
but you can override it by adding exec_args to your gulp settings.
For example, on Ubuntu:
"exec_args": {
"path": "/bin:/usr/bin:/usr/local/bin"
}
Hope It will be OK.
Source: https://github.com/NicoSantangelo/sublime-gulp/issues/12

How do I save all the dependencies I install through npm into my package.json file?

I ran npm install for a lot of packages, but I forgot to include the --save argument. Now when I try to deploy on Heroku I get errors for missing certain dependencies. How can I automatically add those dependencies to my package.json file without doing npm install --save for each one?
You can add all installed packages not installed with --save to your package.json automatically by calling npm init. It will append the dependencies to your existing ones. No settings in your file should be lost. Still don't forget to make a backup of the file to be 100% secure!
If the dependencies have not been appended, it can happen that just the merging failed:
Backup your existing package.json in order to keep the dependencies you have in your package.json already and all the other settings. We need this file later again.
Delete the package.json and run npm init in order to create a new package.json including the modules installed without --save in dependencies.
Merge the dependencies of your newly created package.json into your old one manually. Restore your merged package.json.
Someone already wrote a script for this.
Go to following link
stackoverflow link
here is complete code
run this code inside your project folder
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
main();
It will print all the dependencies inside node_module folder as given below.
"ansi-regex": "2.0.0",
"ansi-styles": "2.2.1",
"asn1": "0.2.3",
"assert-plus": "0.2.0",
"asynckit": "0.4.0",
"aws-sign2": "0.6.0",
"bcrypt-pbkdf": "1.0.0",
"aws4": "1.4.1",
"bindings": "1.2.1",
"bl": "1.1.2",
"boom": "2.10.1",
"caseless": "0.11.0",
"chalk": "1.1.3",
"combined-stream": "1.0.5",
"core-util-is": "1.0.2",
"compress": "0.99.0",
"commander": "2.9.0",
"cryptiles": "2.0.5",
"delayed-stream": "1.0.0",
"dashdash": "1.14.0",
"debug": "0.7.4",
"ecc-jsbn": "0.1.1",
"ejs": "2.3.4",
"escape-string-regexp": "1.0.5",
copy and paste inside your package.json json as follow
{
"name": "test",
"version": "1.0.0",
"main": "server.js",
"dependencies": {
//paste above printed data here
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": ""
}