How to copy package.json to dist folder without the scripts part - javascript

I'm using webpack to bundle my javascript library and I'm currently copying the package.json to the dist folder using CopyWebpackPlugin.
(...)
plugins: [
new CopyPlugin({
patterns: [{
from: "package.json",
to: "dist"
}],
})
],
(...)
I was wondering if I could copy the package.json without the script tag:
{
"name": "monaco-javascript-compiler",
"version": (...),
"description": "(...)",
"main": (...),
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"debug": "npx webpack --config webpack.config.js --mode=development",
"build": "npx webpack --config webpack.config.js --mode=production"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RicardoGomesRocha/monaco-javascript-compiler.git"
},
"author": "Ricardo Rocha",
"dependencies": {(...)},
"devDependencies": {(...)}
}
I know a solution that involves using gulp to read the package.json, remove the script tag from the JSON and output a new file with that JSON, but I'm trying to avoid that.

There is an option called transform in CopyWebpackPlugin,which allows you to amend the content of the file before copying it to the dist folder.
Checkout the documentation here:
CopyWebpackPlugin

Related

NPM npm_package_main variable is always empty

Problem
NPM $npm_package_main variable is always empty.
When I set the package.json file with "main": "index.js"
Set the "start" property from scripts to "start": "node $npm_package_main"
Then run npm start
Problem: the CLI executes the Node REPL mode, ignoring the "main" variable from package.json.
Expected behavior: execute the command as node index.js.
Environment
Linux Ubuntu 20.04.1
npm -v = 7.3.0
node -v = v15.5.0
npm run env | grep npm_package_name = npm_package_name=app
npm run env | grep npm_package_main = EMPTY
How to reproduce
Create an "app" directory and enter the new directory
Create an "index.js" file with the following content
console.log('HELLO');
Run npm init and hit ENTER for all questions
Edit the package.json file and add the following line to the "scripts" property:
"start": "node $npm_package_main",
now your package.json must look like this
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"devDependencies": {},
"scripts": {
"start": "node $npm_package_main",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Run "npm start"
File "index.js" is not executed and Node enters the REPL mode.
Attempts
Set "start" and running "npm start" for:
"echo $npm_package_main" prints nothing
"echo $npm_package_name" prints "app"
"echo $npm_package_version" prints "1.0.0"
References
NPM package.json variables: https://docs.npmjs.com/cli/v7/using-npm/scripts
NPM Github issue: https://github.com/npm/cli/issues/2585
The official answer from NPM: use "node .". There's no official reason for this behavior, until this post.
According to the documentation, the "main" property contains the entrypoint for your app when is used as a module in other projects: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#main
Solution
Use the "config" property instead:
{
"name": "app",
"version": "1.0.0",
"description": "",
"devDependencies": {},
"config": {
"main": "index.js"
},
"scripts": {
"start": "node $npm_package_config_main",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Run "npm start"
It works
References:
https://github.com/npm/cli/issues/2585
https://github.com/npm/cli/pull/2446

Lite-server not working

I had a project that was previously using lite-server. I can no longer get it to run using
npm run
I see the following from the console
PS C:\Users\XXX\Documents\Work\Repos\Homepage_Slider> npm run
Lifecycle scripts included in homepage_slider: test
echo "Error: no test specified" && exit 1 start
lite-server
I don't really understand why this isn't working anymore.
I've tried:
Updating NPM
Running lite-server globably
Make sure package.json is set up correctly
Here is my package.json file
{
"name": "homepage_slider",
"version": "1.0.0",
"description": "A simple slider for homepage",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lite-server"
},
"repository": {
"type": "git",
"url": "XXX"
},
"keywords": [
"Slider"
],
"author": "XXX",
"license": "ISC",
"devDependencies": {
"lite-server": "^2.3.0"
},
"dependencies": {
"npm": "^6.0.1"
}
}
npm run is a reserved word for npm itself. Check the documentation
What you really need is npm start which is a shortcut for npm run start which is a shortcut for npm run-script start!

Using node-modules in main.js using electron-builder and webpack

First of all I do not know if the problem I am having is because of webpack or electron-builder or a combination.
To the problem.
When I build for development I am able to use installed node-modules in the main.js file specified in my package.json file{"main" : "app/main.js"}.
However when I have used electron builder to create an installer. When I have installed the app using the installer I get the following error message when starting the app:
My guess is that I get this message because the the needed node-modules can not be found by main.js. So how do I make them available?
I hope there is someone smarter than me our just better at googling :)
Package.js:
{
"main": "app/main.js",
"scripts": {
"hot-server": "node hot-server.js",
"build-bundle": "rimraf app/dist.release && better-npm-run build-bundle",
"start": "better-npm-run start",
"start-hot": "better-npm-run start-hot",
"backend": "node backend-dummy.js",
"dist-win": "npm run build-bundle && rimraf dist && build --win --ia32",
"dist-mac": "npm run build-bundle && rimraf dist && build --mac"
},
"betterScripts": {
"start": {
"command": "electron ./",
"env": {
"NODE_ENV": "production"
}
},
"start-hot": {
"command": "electron ./",
"env": {
"HOT": 1,
"NODE_ENV": "development"
}
},
"build-bundle": {
"command": "webpack --config webpack.config.production.js --progress --profile --colors",
"env": {
"NODE_ENV": "production"
}
}
},
"bin": {
"electron": "./node_modules/.bin/electron"
},
"build": {
"appId": "app",
"files": [
"dist.release/*",
"dist.resources/*",
"main.js",
"thirdparty/*",
"app.html"
],
"extraFiles": [
"lang/*",
{
"from": "build/extra",
"to": "./",
"filter": "**/*"
}
],
"asar": true
}
So I found a solution to my problem. What I did was I looked at this project:
https://github.com/chentsulin/electron-react-boilerplate
Where they have a special webpack.config.electron.js file that bundles all the node_modules for the main.development.js file into a main.js file that contains everything we need. And then when the electron program starts it uses this main.js bundle to run. To make this happen you need to add a build script in your package.json file that executes webpack.config.electron.js.
I think it is easier to understand how to solve it by looking the linked project than for me to explain.

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.

Node.js nwjs package compilation error: Invalid file descriptor to ICU data

I'm trying to create my first package with nwjs, so I created a simple app with only nwjs module installed locally with npm. My app main file has only a console.log("Hello World!"). I zipped the app files - keeping them in the root - and renamed to app.nw. I basically followed this doc. The package tree is the following:
app.nw
- node_modules
- nw (and all its files inside)
- package.json
- app.js
And my package.json is:
{
"name": "app3",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"window": {
// Some properties here
}
}
Then I created an empty folder and copied app.nw to it. From the module folder /nw/nwjs I copied the files: nw.exe, nw.dll, nw_elf.dll and ffmpeg.dll to it, and dragged app.nw over nw.exe. The debug.log file shows me the following error:
[0524/110408:ERROR:icu_util.cc(157)] Invalid file descriptor to ICU data received.
[0524/110408:FATAL:icu_util.cc(260)] Check failed: result.
I can't find anything related to this error and this tool over the web. I'm running it in a Win7 x64. I also tryied downloading nwjs manually from github and tryied with x64 and x86 binaries, but they all give me the same error.
Below, the backtrace if needed:
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1B7AB1+225089]
std::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >::operator= [0x000007FEDA1640BC+259612]
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1F2E58+467688]
v8::Extension::auto_enable [0x000007FEDA1203D1+368897]
v8::Extension::auto_enable [0x000007FEDA11ED19+363081]
ChromeMain [0x000007FED9CE0EB5+133]
GetUploadedReportsImpl [0x000000013F201251+3297]
GetUploadedReportsImpl [0x000000013F20097F+1039]
IsSandboxedProcess [0x000000013F264628+231640]
BaseThreadInitThunk [0x0000000076C359BD+13]
RtlUserThreadStart [0x0000000076E6A2E1+33]
What am I missing here?
I got it to work with the following package.json:
{
"name": "app4",
"version": "1.0.0",
"description": "",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"single-instance": true,
"package": ".json",
"window": {
"title": "app4",
"toolbar": false
}
}
I'm actually not sure what specific property which I removed/added to made it to work. Then I zipped it to package.nw and put in the same folder as nw.exe to run it.

Categories

Resources