Why can I no longer minify my Javascript files? - javascript

I am using VS Code to maintain a Javascript library I created and I used Google Closure Compiler (CC) to minify the js file. I have made some changes to the original code but although CC executes without any warning/error messages it fails to create the output file.
This is the package.json file
{
"name": "canvas-gui",
"version": "1.0.0",
"description": "On canvas GUI for p5.js",
"main": "main.js",
"scripts": {
"tsc": "tsc",
"typedoc": "npx typedoc",
"gcc": "npx google-closure-compiler src_js --js_output_file dist/lib/gui.min.js --language_in ECMASCRIPT_2020 --language_out ECMASCRIPT_NEXT"
},
"keywords": [
"GUI",
"canvas",
"p5.js"
],
"author": "Peter Lager",
"license": "MIT",
"devDependencies": {
"#types/p5": "^1.4.2",
"google-closure-compiler": "^20220803.0.0",
"p5": "^1.4.2",
"typedoc": "^0.23.10",
"typescript": "^4.7.4"
}
}
I researched on the parameter values here and tried changing the source parameter to
--js src/**.js
--js='src_js/**.js'
src_js/canvas_gui.js
--js src_js/canvas_gui.js
--js='src_js/canvas_gui.js'
all without success.
I also set the warning level to VERBOSE but still no messages.

Related

Server running error in parcel by starting the external file index.html I Ignoring the main but it also doesn't work(Build Failed)

[Build Error in parcel # 2.7.0 start when I am trying to add external index.html file in the main I also ignored the main but it also doesn't work for me. Will please someone help me to solve this problem It also not working with JavaScript file when I added externally and also getting error with html file but I want to add the html file but as a solution I try to add JavaScript file but it also doesn't work for me. Will please someone solve this issue or tell me how to solve this issue
{
"name": "forkify",
"version": "1.0.0",
"description": "",
"main": "unrelated.js",
"targets": {
"main": false
},
"scripts": {
"start": "parcel start index.html",
"build": "parcel build index.html"
},
"author": "Meelad Sultan",
"license": "ISC",
"devDependencies": {
"parcel": "^2.7.0"
}
}
enter code here
At last I can find the solution of my problem after almost a day if someone else facing the same problem they just do add the **Browsers list ** in the package.json file before script and ignore the main target and also set the parcel to latest then the parcel will run successfully and do not give any server error or build error. And don't forget to run periodically npx browerlist#latest --update-db. It will update your package-lock file with new caniuse-lite version. In this case, you will be sure that last two version or > 0.2% will target what you expect
{
"name": "forkify",
"version": "1.0.0",
"description": "",
"main": "unrelated.js",
"target": {
"main": false
},
"browserslist": "> 0.5%, last 2 versions, not dead",
"scripts": {
"start": "parcel index.html "
},
"author": "Meelad Sultan",
"license": "ISC",
"devDependencies": {
"#parcel/transformer-sass": "^2.7.0",
"parcel": "latest"
},
"dependencies": {
"sass": "^1.55.0"
}
}

cant get ES modules to work with import or require

I'm building a command line based application and was using require to link various files and node modules. I have previously used Require with no issues and now I get nothing but errors.
Error with Require:
Error [ERR_REQUIRE_ESM]: require() of ES Module ./lib/departments.cjs not supported,
Instead change the require of inquirer.js in ./lib/departments.cjs to a dynamic import() which is available in all CommonJS modules.
After this I tried multiple methods to get it to work again, but had no luck so I tried to switch to import. When I switched to import I got this message:
SyntaxError: Cannot use import statement outside a module
Ive tried to add type: "module" to my package.json file, but I don't know if I am putting it in the wrong place or what.
My JSON file looks like this
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "server.js",
"directories": {
"lib": "lib"
},
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://issues"
},
"homepage": "https://#readme",
"dependencies": {
"console.table": "^0.10.0",
"dotenv": "^16.0.2",
"express": "^4.18.1",
"inquirer": "^9.1.1",
"mysql2": "^2.3.3",
"node-fetch": "^2.6.7"
}
}
Seems like you are using .cjs extension for your js file, cjs is a commonJS file which doesn't support import. You need to use a dynamic import.
It's worth mentioning that ES modules cannot be imported using the require() function of cjs.

Editing package.js file

i'm having a hard time to understand how to run all of my .js files using package.js files
i have almost 2000.js scripts i need to run them one by one, i'm using a api made by gameflip
in the folder i found package.js, but i don't know how to use it ,
can anyone tell me how to do that ? thank you
here the script :
{
"name": "gfapi",
"version": "0.1.1",
"description": "Gameflip API",
"keywords": "Gameflip",
"homepage": "https://github.com/iJJi/gfapi",
"bugs": "https://github.com/iJJi/gfapi/issues",
"author": {
"name": "Eng-Shien Wu",
"email": "engshien.wu#ijji.com"
},
"license": "MIT",
"private": true,
"files": [
"index.js"
],
"repository": "iJJi/gfapi",
"engines": {
"node": ">=8.5.0"
},
"scripts": {
"bulk_listing": "node src/samples/bulk_listing.js",
"test": "ENVIRONMENT=mocha mocha src/test --recursive",
"docs": "jsdoc -c jsdoc_conf.js -d docs -P package.json index.js; docco -o docs/samples src/samples/*.js src/samples/*.rb"
},
"dependencies": {
"base-64": "^0.1.0",
"bluebird": "^3.5.0",
"bunyan": "^1.8.12",
"file-type": "^8.1.0",
"http-errors": "^1.6.2",
"node-rest-client-promise": "^3.1.1",
"promise-ratelimit": "^0.0.3",
"request": "^2.85.0",
"request-promise": "^4.2.2",
"speakeasy": "^2.0.0"
},
"devDependencies": {
"marked": "^0.3.19",
"docco": "^0.7.0",``
"jsdoc": "^3.5.5"
}
}
What you posted isn't a package.js (I don't even know if it exists), but a package.json. It's generated by NPM, the Node Package Manager. It's a list of all the project's dependencies. I think that what you're looking for are the npm scripts, they are in the script object of package.json.
npm run <script>
# For example :
npm run bulk_listing
npm run test
npm run docs
Each script will run its associated command in this package.json.
npm run bulk_listing
# Will do the same thing as:
node src/samples/bulk_listing.js
More about package.json.
The script I talked about below
If you want to run all the scripts, this should do the job :
const fileNames = ["path/to/fileA", "fileB"]; // I assume you have something to get all the files path. Isn't that npm run bulk_listing ?
fileNames.forEach(async (path, index) => {
// It's pretty much like 'node <path>'
await require(path);
// All the code here is executed AFTER the script has been launched
console.log(`LAUNCHED ${index} | ${path}`)
});

Why do we need to use package.json? [duplicate]

This question already has answers here:
What are the main uses for the NPM package.json file?
(2 answers)
Closed 6 years ago.
I am trying to learn web socket with JavaScript to this site, but I do not understand something that why I need to use package.json. This is my package.json file code:
`{
"name": "module-name",
"version": "10.3.1",
"description": "An example module to illustrate the usage of a package.json",
"author": "Your Name <you.name#example.org>",
"contributors": [{
"name": "Foo Bar",
"email": "foo.bar#example.com"
}],
"bin": {
"module-name": "./bin/module-name"
},
"scripts": {
"test": "vows --spec --isolate",
"start": "node index.js",
"predeploy": "echo im about to deploy",
"postdeploy": "echo ive deployed",
"prepublish": "coffee --bare --compile --output lib/foo src/foo/*.coffee"
},
"main": "lib/foo.js",
"repository": {
"type": "git",
"url": "https://github.com/nodejitsu/browsenpm.org"
},
"bugs": {
"url": "https://github.com/nodejitsu/browsenpm.org/issues"
},
"keywords": [
"nodejitsu",
"example",
"browsenpm"
],
"dependencies": {
"primus": "*",
"async": "~0.8.0",
"express": "4.2.x",
"winston": "git://github.com/flatiron/winston#master",
"bigpipe": "bigpipe/pagelet",
"plates": "https://github.com/flatiron/plates/tarball/master"
},
"devDependencies": {
"vows": "^0.7.0",
"assume": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0",
"pre-commit": "*"
},
"preferGlobal": true,
"private": true,
"publishConfig": {
"registry": "https://your-private-hosted-npm.registry.nodejitsu.com"
},
"subdomain": "foobar",
"analyze": true,
"license": "MIT"
}`
But I do not understand why I need to use it. I tried to find on google but can not find something useful. One more thing, if I create package.json then why I need to write dependencies?
You do not NEED package.json. However, you will most likely want it. As you make larger applications using node.js, you will find that using other people's code is the best solution vs. writing the same thing yourself.
npm has lots of packages that people want to use over and over again. package.json provides a simple way for people to keep track of packages they use in their application.

node_modules is not recognized as an internal or external command

I'm trying to write a test automation script using appium, jasmine, and perfecto mobile. I'm using the project cloned from the following URL with my own configuration Appium Javascript Example
The problem is when I execute the npm test command I get the following error
node_modules is not recognized as an internal or external command
This is how the packages.json script looks like:
{
"name": "perfecto_appium_sample",
"version": "1.0.0",
"description": "The following sample shows how to Install an application and use WebDriverIO to automate and test it.<br/> It uses selendroid test application which can be downloaded from [here](https://github.com/PerfectoCode/AppsForSamples/tree/master/selendroid-test-app-0.17.0).",
"main": "perfectoSpec.js",
"scripts": {
"test": "node_modules/webdriverio/bin/wdio wdio.conf.js",
"start": "wdio wdio.conf.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"wdio": "^0.3.3",
"wdio-jasmine-framework": "^0.2.19",
"wdio-mocha-framework": "^0.5.12"
},
"dependencies": {
"wd": "^1.5.0",
"webdriverio": "^4.10.2"
},
"keywords": []
}
you need to provide relative path properly:
"scripts": {
"test": "node ./node_modules/webdriverio/bin/wdio wdio.conf.js",
"start": "wdio wdio.conf.js"
}
Just remove the paths "node_modules/webdriverio/bin/" and simply specify "wdio wdio.conf.js". It should work.

Categories

Resources