Can I use NPX with yarn? - javascript

I have a project that I am using yarn for. I've installed all my packages using yarn, I run yarn dev and so on. Now I'm following a tutorial that requires me to use npx to set up a package. - I'm wondering if I can just go ahead with this or if I will end up mixing things up here, as npx, as far as I know, is related to npm ?

Yes. npx will run the executable from your node_modules directory if it is installed there. If it is not, it will install the executable into a different location. It will not interfere with yarn operations.

Related

Lerna error: cannot find module during CI build

I have a monorepo using Lerna that I trying to build with Gitlab CI. Running lerna run build locally builds everything successfully.
The Dockerfile that Gitlab tries to execute looks a bit like this:
FROM node:16 AS common
WORKDIR /usr/src/app
RUN yarn global add lerna
COPY . .
RUN lerna bootstrap --include-dependencies
RUN lerna link
RUN yarn
# yarn build === lerna run build
RUN yarn build
Which results in the following errors:
Cannot find module '#project/common' or its corresponding type declarations.
import { SomeClass } from '#project/common';
Is there any step I am missing? Thanks in advance!
What version of lerna is being used locally? Amend your question to include the output of learna info.
yarn global add learna will install the latest version, which currently is v4.0.0.
I was able to fix this issue by changing the Dockerfile. Running yarn before running lerna bootstrap and removing yarn build resolved the errors.
My Dockerfile now looks like this:
FROM node:16 AS common
WORKDIR /usr/src/app
RUN yarn global add lerna
COPY . .
RUN yarn
RUN lerna bootstrap --include-dependencies
I can definitely attribute the errors to my limited knowledge of Lerna, but I hope this answer can still help others with similar issues.

How to run Typescript files on Server in background?

I have a NodeJS application which has only typescript files in it, no js files. I want to run in on my server in the background.
How can I archive that?
I tried using the npm package called forever but it only works with js files as it doesn't understand the typescript code.
You could use forever in combination with ts-node.
Here is a link to the npm package ts-node
I would suggest using the following command:
forever start -v -c ts-node app.ts
Where -v/--verbose is for turning on the verbose messages from Forever.
And -c is the COMMAND to execute which is default is node
This question is so old and forever now discourages to use it.
For new installations we encourage you to use pm2 or nodemon
Here is a quick tutorial on how to run your Typescript project as a background process.
Install PM2 globally:
npm install pm2 -g
Build your sources with Typescript default config:
tsc
You will have a new directory dist that contains your js files.
pm2 start dist/app.js
Bonus: you can monitor your app with the following command.
pm2 monit
first use
npm install -g ts-node
then use
forever start -v -c ts-node app.ts
it shuld start now
The two production quality recommendations I would make are:
Turn it into a docker container
Write a systemd service
Those are by far the best options. If for some reason this doesn't work:
pm2
supervisord

How to install multiple gulp packages at once using node?

I just switched to gulp task runner to automate my workflow, but there is this problem whenever i want to start a new project i have to install all packages required in gulpfile.js using the following command:
npm install --save-dev {package name}
Imagine there are 20 of them, it's a bit boring. How can simplify this?
You can add multiple package names to npm install:
npm install --save-dev package1 package2 package3
npm will install and save the specified packages in your package.json.
Personally I use mostly the same gulp plugins for all of my projects. I copy the devDependencies bit from the package.json of one of my previous projects into my newly created package.json, then I run npm i which installs all dependencies listed in package.json. It's a huge timesaver, especially since I usually copy my gulpfile.js as well.
Note: don't forget to run npm outdated if it's been a while since your previous project started, to check if any of the dependencies have been updated in the meantime.
You can also use brace expansion for installing many similarly named packages:
npm i -D babel-{core,preset-es2015,preset-react}

install optimise with npm from optimist.js file

Trying to fix an offline install of the Carto tool from Mapbox on Ubuntu 14.04. Currently, node is giving me an error, it cant find the optimist module. I can manually find an optimist.js file, but its not under a "proper" node_modules directory like the other Carto dependancies are (e.g. underscore). I'm very new to npm and node, so my question is "is there a way to properly install optimise from this optimist.js file I have?"
If you just want to use the package,
$ npm install -g carto
should be enough to get you started. Note that you might need to use sudo for that command.
Node.js is centered around a package manager called "npm". Every npm project has a package.json at its root directory that specifies its dependencies, package name, version, et al. By looking at carto's package.json even if you installed optimist successfully you will encounter some more require errors.
If you want to develop the package, after cloning it with Git run
$ npm install
inside the directory. That will install all dependencies (including devDependencies that are not installed when you are installing the package itself) for you.
There are plenty of tutorials on how Node.js works, like http://nodeguide.com/beginner.html. Those should give you a more comprehensive view than this answer.
With a proper package manager like npm, you should never using a random script found on the internet to fake it as a module.
Good luck!
UPDATE now that one knows how npm works, if you want to make it available locally to be installed, you can do something like this
# With Internet
# Make a cache
$ mkdir carto-cache
# Make a temporary directory where the initial copy of carto is installed
$ mkdir to-be-discarded
$ cd to-be-discarded
# Now install the package, and cache the package in carto-cache
$ npm install --cache ../carto-cache --prefix . carto
# You can now remove to-be-discarded, and copy carto-cache to wherever you want.
# Without internet
$ npm install --cache carto-cache --cache-min 999999999 -g carto
(derived from https://github.com/npm/npm/issues/2568)

What the difference between install a module with npm node console or in package.json

I'm new and I need to use node for off-line use so, I'm trying to understand how the install modules work.
It's the same if I use npm install express or included it in the package.json?
The way a package is installed will be the same whether you manually type npm install express or put it in your package.json and then do npm install. The difference comes when you try to install your Node project elsewhere.
For example, if your code was checked into GitHub and you didn't include a package.json with all of the dependencies listed, then when the project was downloaded you would have to manually re-install all of the dependencies on the command line in order for it to work. But if you had checked in a package.json with the code, then you could run npm install to install all of the dependencies at once, and not have to remember which ones were necessary.
In addition, the package.json allows you to specify an "approximate version" of a dependency to use. This way if a few packages in your project share a dependency and they all specify similar "approximate versions", only one version will be installed and it will be shared between packages. This saves some install time.
Nothing actually. But you don't want to do that again and again. So you might as well put your module dependencies in your package.json

Categories

Resources