pm2 cannot start express application - node.js

I wrote a simple Express application. It ran correctly with "npm start". The scripts in package.json is this:
"type": "module",
"scripts": {
"start": "src/bin/www.js"
}
But I couldn't execute it like this:
pm2 start ./src/bin/www.js
I checked pm2.log. It shown "had too many unstable restarts(16)".
Then ran it with pm2-dev:
pm2-dev start ./src/bin/www.js
Reported the error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
Pm2 can't work well with import/export? The node version is 14.16.0.

First make sure if you have following two points:
Node version >= 14. It only works with latest version of node.
Make sure your package.json includes a line for "type": "module", Without this line node assumes you want to use Common JS modules rather than ESM.
If both are true and it's still not working then you might be victim of following github issue
https://github.com/Unitech/pm2/issues/4540
For now you can try running it with babel!

Related

select the commonjs file when running under commonjs of a dependency when running nodemon

I am running node 14.
I have a package that has this in the package.json
"main": "dist/index.js",
"type": "commonjs",
There are no browser or module fields.
It is a typescript project that I compile to a .cjs output.
The problem is that when I start nodemon like this:
nodemon --watch 'src/**/*' -e ts,tsx --exec ts-node ./src/index.ts
I get this error:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/blah/dist/index.js
The module in question has module, 'mainandbrowserfields but its type ismodule`.
It has index.js and index.cjs files but index.js file is being chosen.
everything is fine when I run the compiled output that which is a .cjs file.
Is there a way to let node know that I want everything to run under commonjs.
I don't think there is a way to have node do what you want. I had a similar where the main project was commonjs but one of the libraries was esm or a library it was using was esm. I don't remember the specific details but it was a royal pain.
The basic work around is to use esm to import the library giving you issue. It likely that once you do that you will end up with a second layer of issues.
// example
const esmImport = require('esm')(module)
const {CookieJar, fetch} = esmImport('node-fetch-cookies')
Dynamic import also resolved the issue for me.
async init() {
const { CookieJar } = await import('node-fetch-cookies')
this.cookieJar = new CookieJar()
this.cookieJar.addCookie(`username=${this._user}`, this._url.toString())
this.cookieJar.addCookie('hippa=yes', this._url.toString())
}
I had the same issue and I used single quotation for multi-value args like `--exec' to solve it.
nodemon --watch 'src/**/*' -e ts,tsx --exec 'ts-node ./src/index.ts'

Google Cloud Functions error: "Cannot find module 'sharp'" but it's in my package.json

I am trying to deploy a function to Google Cloud Functions. I based it on their ImageMagick tutorial.
Every time, the function fails to deploy because it reaches an error. Looking at the log, the error is:
Provided module can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace:
Error: Cannot find module 'sharp'
I can't figure out why this is happening, because sharp is in my package.json dependencies. If I open the web editor for the function in the Google Cloud console, the package.json is there as one of the files and shows sharp as a dependency. I tried running npm install and npm install --save and re-deploying, and that hasn't fixed anything.
I'm including the package in the function with const sharp = require('sharp'); (this is the line where the log shows the error occurring), and this is my package.json:
{
"name": "Resize images",
"version": "0.0.1",
"private": true,
"author": "James Tyner",
"engines": {
"node": ">=10.0.0"
},
"dependencies": {
"#google-cloud/storage": "^5.0.0",
"sharp": "^0.25.4"
}
}
Can you help me figure out what I'm doing wrong?
This has happened to me many times since I was tricked to install packages in the project directory. It works fine locally but creates an error when you try to deploy.
It worked for me when I changed directory into the functions folder, instead of the firebase project folder and did a package install in there
cd functions
npm install [your missing package] --save
I was running into this issue. Various dependencies were causing my function deployment to fail. After a bit of digging I found that the peer-dependencies were not being included.
Adding this fixed my issue
"scripts": {
...
"gcp-build": "npm i npm-install-peers"
},
checking the docs.
the gcp-build command allows us to perform a custom build step during the function build process.
Somehow I was able to address the issue, but I don't fully understand what I did differently. I found that the dependencies listed in package.json weren't being installed when I ran npm install, so I created a separate folder and copied my code there, ran npm install in the new folder, and it worked well from there. Since then, the dependencies have been working properly when I change them and re-deploy the function.
Using Node v12.13.1 and serverless deployment with webpack to GCP and cloud-functions, I've struggled with this issue. In my case it was a different module though. The problem is that no module from node_modules will be possible to require or import. The reason becomes clear if one takes a look at the webpack zip-file in directory .serverless. It seems that with GCP nothing but the file (typically index.js) denoted as "main" in package.json is actually included.
The solution was to adapt webpack.config.js to explicitly include those files missing.
webpack.config.js

'standard' is not recognized as an internal or external command

I want to integrate some kind of code linting for node.js in webstorm so I installed standard to my node.js project using:
npm instal standard --save-dev
It was installed and listed in the "devDependencies" section of package.json but when I run the command:
standard
in the console I get
'standard' is not recognized as an internal or external command
if you want to use it locally you have to include it in you scripts first in package.json
"scripts": {
"standard": "standard",
"standard::fix": "standard --fix"
}
and use npm run standard to run it. or if you are using yarn type yarn standard
The scripts are in node_modules\.bin.
So, either:
Add this to PATH before running standard, e.g.:
set PATH=%PATH%;node_modules\.bin
Run it in using node_modules\.bin\standard
Use #tarek's approach using package.json: https://stackoverflow.com/a/49026837/122441
"scripts": {
"test": "standard middlewares/validations.js"
}
Add above lines in package.json.
Here middlewares/validations.js is the path of the file to check.
Run -> npm test
If this file have any error you will get.

Run node module from powershell

Say that I have a folder with a node module installed inside. How can I, using powershell, run this module ? For example, I have a folder with webpack installed inside and I'd like to be able to do something like
node webpack
This does not work because it can't find the module. However, it works if I do:
node ".\node_module\webpack\bin\webpack.js"
However, I can't really use this because I'm working on a powershell script that allows the user to define a json file with actions. For example:
{
"type": "run-node",
"options": {
"module": "webpack",
"parameters": "--configFile=config/webpack.prod.js"
}
}
As the module to execute is dynamic, I don't know where it will be (except if all module are installed in ".\node_modules\module\bin\module.js" ?), so I can't launch it.
You can define a webpack script in your package.json:
"scripts": {
"webpack": "node \"./node_module/webpack/bin/webpack.js\""
}
Then you can use npm run webpack or yarn webpack to run it.
If you're trying to execute the command within the node.js script, use the __dirname variable.
__dirname (almost) always refers to the directory that contains the script.

Write and run node.js application in Typescript

I searched for this a day now and hope you can help me.
I really like developing JS Applications in TypeScript and I am trying to write my new node application that way. Unfortunately node does not understand TS. Is there a way (besides transpiling to ES5) to start my code directly from the TS file?
In the sad case there is no way to do that, what is the best practice? Writing the app in src/ts/app.ts, transpile it to src/js/app.js and reference this file in the package.json?
Exactly. Simply run tsc before starting your node application. You typically do that by having something like this in your package.json
{ ...
"main": "src/js/app.js",
"scripts": {
"run": "tsc && node src/js/app.js"
}, ... }
For your further reference, there is a node sample from the official Typescript repositories available here.

Resources