NPM build phase of my package not executed during install - javascript

I am developing a package to use web workers in a generic way but I am finding some issues when trying to add it as a dependency for another project.
Normally I would expect that having a build script section of my package.json when doing install that it would be automatically called generating the output of the rollup.config.js. But it does not seem to execute anything. Do I have any misunderstanding on how npm build should be working?
If not, there be any other colliding script in package.json that is causing it not to work in the next file example:
{
"name": "web-threads",
"version": "1.0.5",
"description": "generic threads using web workers for the web",
"main": "dist/web-threads.js",
"scripts": {
"build": "rollup -c",
"test": "jest",
"test:dev": "jest --watchAll test/unit",
"test:int": "jest test/integration",
"test:cov": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"push": "yarn test && git push",
"deploy:major": "yarn version --major",
"deploy:minor": "yarn version --minor",
"deploy:patch": "yarn version --patch",
"deploy:push": "git push && git push --tags",
"preversion": "yarn test"
},
"keywords": [""],
"repository": "",
"author": "",
"license": "MIT",
"private": false,
"devDependencies": {
"babel-jest": "23.4.2",
"babel-preset-env": "1.7.0",
"babel-preset-stage-0": "6.24.1",
"coveralls": "3.0.2",
"faker": "4.1.0",
"jest": "23.5.0",
"jest-puppeteer": "3.3.1",
"puppeteer": "1.7.0",
"rollup": "0.65.0",
"rollup-plugin-babel": "3.0.7",
"rollup-plugin-uglify": "4.0.0",
"uglify-es": "3.3.9"
},
"babel": {
"presets": ["env","stage-0"]
},
"jest": {
"testMatch": [
"**/test/**/*-test.js"
],
"transform": {
"^.+\\.jsx|.js?$": "babel-jest"
}
}
}
I also moved the dependencies to not be devDependencies but it didn't help solving the issue.
NPM build documentation: https://docs.npmjs.com/cli/build

You could try adding a postinstall script.
As documented in the npm docs
postinstall: Run AFTER the package is installed.

So the answer of #Olian04 send me in the right direction and dig a bit on the documentation. Indeed I had a misunderstanding regarding build as it is actually not a script but just a hook to the process stage.
So seems the correct way of solving the compilation required in packages is a different process run by prepare. This is a script that documentation defines as:
For build steps that are not platform-specific, such as compiling CoffeeScript or other languages to JavaScript, use the prepare script to do this, and make the required package a devDependency.
The prepare script will be run before publishing so that users can consume the functionality without requiring them to compile it themselves. In dev mode (ie, locally running npm install), it'll run this script as well, so that you can test it easily.
Example:
{ "name": "web-threads",
"description": "a delightfully fruity coffee varietal",
"version": "1.2.3",
"devDependencies": {
"coffee-script": "~1.6.3"
},
"scripts": {
"prepare": "coffee -o lib/ -c src/waza.coffee"
},
"main": "lib/waza.js"
}
As a summary use postinstall for things that need to happen locally to the installing computer/platform (but it will require all dependencies to be satisfied). Use prepare for process that are not platform dependant this will not require the user to have all the tools to trnspile the package and you will also not polute your repository.

Related

Node.js install Python dependencies from within package.json

I'm building a Node.JS project that uses both Python and NPM dependency modules.
The Node.JS modules are located within package.json and the python dependencies are inside a file requirements.txt.
I would like to install all the dependency modules (Python and Node.JS) from within package.json by running npm install.
Is this possible and how?
Thanks in advance!
The files look like below.
package.json:
{
"name": "app",
"version": "1.0.0",
"description": "Trial app",
"main": "bin/index.js",
"scripts": {
"dev": "npm install",
"start": "node app.js",
"test": "jest --forceExit "
},
"keywords": [
"AI",
"Blockchain",
"Decentralized"
],
"dependencies": {
"peerjs": "^1.3.2",
"redis": "^3.1.2",
"socket.io": "^4.1.2",
"socket.io-client": "^4.1.2",
"wrtc": "^0.4.7"
},
"devDependencies": {
"#babel/core": "^7.16.7",
"supertest": "^6.1.6"
}
}
requirements.txt:
Django==2.2.21
djangorestframework==3.7.7
django-rest-swagger
coreapi
You can define Commands to run within the "scripts" section of your package.json. Every script in there can be run with npm run [scriptname].
So you could do (&& runs another command after the first one)
"scripts": {
"install": "npm install && pip -r requirements.txt",
"dev": "npm install",
"start": "node app.js",
"test": "jest --forceExit "
}
And run npm run install
Replace "dev": "npm install" with "dev": "npm install & pip install"
Add "preinstall" entry to scripts.
npm, yarn and pnpm will automatically execute preinstall script before installing dependencies and 'devDependencies`:
package.json:
{
"scripts": {
"preinstall": "echo 'Installing Python Dependencies...' && pip install -r requirements.txt && pip install -r subproject/requirements.txt"
},
...
}
To install both npm and python dependencies, just run:
$> npm install
Installing Python Dependencies...
Also, there are other hook scripts in npm process, like "prepare", which might be useful. And scripts can be chained with ... && npm run <script>, so the "scripts" section can be organized into small atomic ones and built up by chaining them. I use "scripts" as a project's build and deploy active knowledgebase, replacing make file functionality not only in JS, but even in pure Python projects.
It is also possible to hook "package.json" into python script, i.e. create something like "build_project.py" script (or whatever name that works for you, I've used "make.py" and "build.py" for less typing) specific to the project, add all python-related stuff there, and run the npm commands from it. It may be more coding than using "scripts" section in "package.json".

Is packaging an Electron-builder via npm (not yarn) possible?

I have create a python3 app using the Electron environment (itself based on Node.js).
For this app, the dependencies have been installed using npm and my app launches using the npm start command.
Following the documentation of Electron, it seems that Electron-builder is the best option for distributing my app for all platforms. Unfortunately, the installation guide recommends using yarn, not npm as npm might not be working properly : see related ticket
Now my question is: As the ticket is rather old (2017), is it now possible to make it work with npm ? So yes how ?
If it is not possible to make it work with npm, how do I transition my code to yarn ?
For info:
The reason I ask is because I get a BIIIIIG error message when I run the command npm run dist.
If needed I can edit the post to include it (alongside my package.json file)
Thanks in advance.
EDIT: adding error and package.json
{
"name": "APP_NAME",
"version": "1.0.0",
"description": "The first version of APP_NAME's GUI.",
"main": "index.js",
"homepage": "https://docenhance.com",
"author": "DocEnhance <nootaku#gmail.com>",
"license": "MIT",
"scripts": {
"postinstall": "electron-builder install-app-deps",
"start": "npm install && electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
"build": {
"appId": "com.docenhance.APP_NAME",
"productName": "APP_NAME - PROJECT_NAME",
"linux": {
"target": "deb",
"icon": "build/icon.icns",
"category": "Office"
}
},
"devDependencies": {
"bootstrap": "^4.1.2",
"electron": "^4.0.7",
"electron-builder": "^20.39.0",
"jquery": "^3.3.1"
},
"dependencies": {
"bootstrap": "^4.3.1",
"jquery": "^3.3.1",
"popper.js": "^1.14.7",
"python-shell": "^1.0.7"
}
}
The error file can be found HERE.
Yes, you can use npm. The yargs issue is explained here.
Your error messages tell you . . . what the errors are.
error output:[ERROR] Unknown input file format:
/home/MYUSERNAME/Documents/APP_DIRECTORY/dist/.icon-set/icon_ICN#.jp2
Known file formats are *.j2k, *.jp2, *.jpc or *.jpt
I don't use or work on Linux so I don't know for sure if this is the case, but you are specifying a .icns file format for your icon. That is an OSX icon file format. Your error message says that Electron Builder doesn't know what to do with that (on Linux).
"linux": {
"target": "deb",
"icon": "build/icon.icns",
"category": "Office"
}
The rest of the scary error text likely just cascades from that.

npx ts-lint cannot find module 'typescript' when run in docker

I'm setting up a dockerized dev environment for node/typescript for an api project. The goal is to run everything in docker and not have any of installed node, npm or modules installed on the host. This is to isolate all versions of node and all modules from other projects.
./node
docker run \
-it \
-p "8080:80" \
--rm \
-w "/app" \
-v "$(pwd):/app" \
"node:10" "$#"
./npm
#!/bin/sh
./node npm $#
./npx
#!/bin/sh
./node npx $#
./package.json
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx ts-lint --project src $#"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"#types/node": "^10.12.15",
"ts-lint": "^4.5.1",
"typescript": "^3.2.2"
}
}
[edit]
I use ./npm install to build my node_modules. The node_modules is in a shared volume so it persists on the host after the container is removed. That way I don't need a Dockerfile to build an image.
[/edit]
When I run the lint command, I get the following error:
testapi$ ./npx ts-lint -i
10: Pulling from node
Digest: sha256:5af431757f84bf7878ff72447eb993fc37afcd975874fff13278157bf83661e6
Status: Image is up to date for docker-remote.registry.kroger.com/node:10
npx: installed 32 in 2.883s
Cannot find module 'typescript'
I think this has to do with module resolution, but I don't know this for sure. I see people install typescript globally, but that would mean I have to do a Dockerfile instead of using the stock node image. I don't mind using a Dockerfile for dev, but I think there should be a way to make this work without doing that.
So I figured out the answer. It wasn't obvious, and I stumbled upon it by accident.
I had installed ts-lint (see package.json above), and I saw an example which referenced tslint (without the hyphen).
So I removed ts-lint and installed tslint and it worked like a champ. I'm not sure what the difference is, but the one with the hyphen does not work in my project configuration. Also, the one without the hyphen installed a higher version number than the one with the hyphen.
See my new package.json containing the working dependency:
{
"name": "testapi",
"version": "0.0.1",
"description": "a hello world api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "npx ts-node src/app.ts",
"lint": "npx tslint --project ./ 'src/**/*.ts?(x)' $#"
},
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"fastify": "^1.13.2",
"ts-node": "^7.0.1"
},
"devDependencies": {
"#types/node": "^10.12.15",
"tslint": "^5.12.0",
"typescript": "^3.2.2"
}
}
This works when run in a docker container, just using the public node:10 image. It doesn't need a Dockerfile to install any global dependencies.
Try
yarn global add tslint typescript
or if it complained for permissions:
sudo yarn global add tslint typescript

Issue when I want dockerize my nextjs project

I encountering an issue to dockerize my nextjs (it's a library to SSR ReactJS) project. When I tried to docker-compose up my application, it fails on step 6/8. Apparently, my flowtype plugin plugin-transform-flow-strip-types isn’t manage by the build process. That plugin was added to my package.json and on my .babelrc file. However, everything was fine when I use node start to launch my project or node build to build my project with nextjs. The problem is linked to docker.
Here my Dockerfile πŸ‘‡
FROM node:10.13.0
RUN mkdir -p /website
COPY . /website
WORKDIR /website
RUN yarn install --production=true
RUN yarn run build
EXPOSE 3000 9229
CMD [ "yarn", "run", "start" ]
Here my docker-compose.yml πŸ‘‡
version: "3"
services:
app:
container_name: website
build: .
ports:
- "3000:3000"
- "9229:9229"
Here my .babelrc file πŸ‘‡
{
"presets": [
"next/babel"
],
"plugins": [
"#babel/plugin-transform-flow-strip-types"
]
}
Here the cli output when I run docker-compose πŸ‘‡
{
Error: (client) ./pages/index.jsx
Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-babel-loader.js):
Error: Cannot find module '#babel/plugin-transform-flow-strip-types' from '/website'
}
Here my package.json πŸ‘‡
{
"name": "XXXXXXXX",
"description": "XXXXXXXX",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "XXXXXXXXXXXXXXXX.git"
},
"scripts": {
"dev": "next -p 3000",
"build": "next build",
"start": "next start -p 3000",
"lint": "eslint . --ext .js --ext .jsx",
"lint-fix": "eslint . --ext .js --ext .jsx --fix",
"test": "jest --notify",
"flow": "flow"
},
"dependencies": {
"next": "^7.0.2",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-apollo": "^2.2.4",
"apollo-boost": "^0.1.20",
"graphql": "^14.0.2"
},
"devDependencies": {
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"eslint": "^5.8.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-flowtype": "^3.2.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.11.0",
"flow-bin": "^0.85.0",
"jest": "^23.6.0"
}
}
Do you have any idea to fix the problem?
Thanks!
It looks like you're depending on the wrong package, you're depending on the babel 6.x version of plugin-transform-flow-strip-types, whereas in code, you're requiring the babel 7.x version.
Run the following command to depend on babel 7.x version:
npm install --save-dev #babel/plugin-transform-flow-strip-types
Lastly, remove the old version from your dependencies with:
npm uninstall babel-plugin-transform-flow-strip-types

Error when building my electron app

I'm stuck with a problem when building my Node JS/electron app with electron-builder. It works perfectly when running npm start. But, when I execute the command build -w (for windows) it fails with this log.
Here is my JSON file:
{
"name": "Munshiiii",
"version": "1.0.0",
"description": "This is a short Description of the project",
"main": "index.js",
"scripts": {
"start": "electron .",
"dist": "build -w"
},
"author": "Hicham Dbiza",
"license": "ISC",
"devDependencies": {
"electron": "1.7.8",
"electron-prebuilt": "^1.4.13"
},
"dependencies": {
"asar": "^0.13.0",
"cradle": "^0.7.1",
"fs": "0.0.1-security",
"git": "^0.1.5",
"jquery": "^3.2.1",
"jsdom": "^11.3.0",
"loke-ipc": "^1.0.5",
"mongodb": "^2.2.33",
"node-couchdb": "^1.2.0",
"pouchdb": "^6.3.4",
"pouchdb-replication-stream": "^1.2.9",
"scanner.js": "^1.0.0"
},
"build":{
"appId": "com.hicham.dbiza.faizan",
"win":{
"target": "nsis",
"icon": "build/Munshiiii.ico"
}
}
}
for this project Im using:
fs
pouchdb
electron
jquery
....
I already used electron-packager and it works almost fine with one problem: See this picture, which means all links inside the js files (e.g: fs.readFileSync('./assets/state','utf8')) won't work.
I have also added some native js click and keypress listeners... could that be a problem?
My electron version is 1.7.8.
I appreciate your help.
Yarn is strongly recommended instead of npm.
yarn add electron-builder --dev
if you are using Npm
do just simple steps terminal:
1 npm install yarn -g
2 yarn
3 yarn pack
Read this Blog (reactJS in electronApp with .exe file)
Have you installed electron-builder? You don't have it in your package.json. Then I would propose to use the electron-builder command, as recommended by the authors.
Run npm install electron-builder --save-dev and change your dist command to run just electron-builder. Since electron-builder per default build for the current running OS, it's not necessary to send the -w flag. If you still experience problems, try to set the following env variable to get a better stack trace:
DEBUG=electron-builder,electron-builder:*
Edit after getting more information from the comments:
According to this issue at GitHub your first issue seemed to be caused by permission errors, and was solved with running as administrator.
From electron-builders README:
Yarn is strongly recommended instead of npm.
yarn add electron-builder --dev
Try to remove your node_modules folder and run
npm install yarn -g && yarn && yarn pack
First I installed electron-builder with following line
npm install -g electron-builder
then I created a build folder in the root of my project, containing the .ico file with the app logo. then I wrote following parts in my package.json
"build": {
"appId": "your.app.id",
"productName": "Your final product name"
}
and
"scripts": {
"start": "electron .",
"pack": "build --dir"
}
That was everything I needed. I opend my root folder in the terminal and executed
build -w
That created an folder called dist, with an unpacked version of my app plus an installer for the app.

Categories