How to get the sources of a npm package locally when developing? - npm

I am very new to node and npm and I am wondering if it is possible to work on a application, but also work on a local git repository for some node modules.
Let's say I am starting a new project that uses chalk and I would like to develop both on my project and on chalk.
mkdir my-project && cd my-project
npm install
npm install --save chalk/chalk
With the above command I will the source of chalk into node_modules but I cannot contribute to it.
Is there a way to directly get the Git repository?

Asumming '~/projects' is your projects folder, first clone chalk:
cd ~/projects
git clone https://github.com/chalk/chalk.git
Then create your project:
mkdir my-project && cd my-project
npm init
And set your project to use your local chalk. Look for package.json file inside my-project and include chalk in dependencies:
"dependencies": {
"chalk": "file:../chalk"
....
....
Then install chalk in your project:
cd ~/my-project
npm install chalk
Or simply 'npm install' to install all your dependencies. Every time you need to include in your project changes you made on chalk type again 'npm install chalk'. It copies ~/chalk on ~/my-project/node_modules and uses it.
You can continue working on your project and on chalk.

Related

How does react-scripts install extra dependencies?

To reproduce what I'm talking about
Create an empty directory
cd into the directory and run npm init
run npm install react-scripts
look at the node_modules directory. react-scripts exists inside node_modules, but it also installs many other dependencies required to run a project with create-react-app.
Looking at react-scripts directory in node_modules, I don't see any pre or post install scripts. I do see a react-scripts/bin/react-scripts.js script that im assuming is the entrypoint for the code installing these extra dependencies.
How is that file being run?

Understanding difference between npx and npm

I'm struggling to develop a deeper understanding of npx. So in particular the difference between running a commmand with npm and npx. I understand that npx can execute a package from a URL, just one local npm package etc.. But for example here:
npx lerna run start --scope frontend --stream
What is the difference between
npx lerna run start
and
npm lerna run start
?
NPM - Manages packages but doesn't make life easy executing any.
NPX - A tool for executing Node packages.
NPX comes bundled with NPM version 5.2+.
NPM by itself does not simply run any package. It doesn't run any package as a matter of fact. If you want to run a package using NPM, you must specify that package in your package.json file.
When executables are installed via NPM packages, NPM links to them:
1.local installs have "links" created at ./node_modules/.bin/ directory.
2.global installs have "links" created from the global bin/ directory (e.g. /usr/local/bin) on Linux or at %AppData%/npm on Windows.

After npm run build > dependency was not found VueJs

After
npm run build
When serving with
npm run serve -s dist
This dependency was not found:
/Users/zarpio/code/vuejs/p3-admin/dist in multi (webpack)-dev-server/client?http://192.168.31.234:8081/sockjs-node (webpack)/hot/dev-server.js ./dist, multi (webpack)/hot/dev-server.js (webpack)-dev-server/client?http://192.168.31.234:8081/sockjs-node ./dist
To install it, you can run: npm install --save /Users/zarpio/code/vuejs/p3-admin/dist
I believe there is a dependency your project has which you have not installed. Run npm install to install the necessary dependencies. sometimes npm gets messed up and i need to delete the entire node modules folder and then run npm install to get a fresh node modules setup.
just to clear some things up.the default vue cli configuration is that npm run serve will run your app live locally. the npm run build compiles your app into the dist folder which you then can serve from your server ( or something like the live server extension in VS code)

npm 5 install folder without using symlink

Before publishing my node library, I could use the advice the npm documentation wrote about:
To test a local install, go into some other folder, and then do:
cd ../some-other-folder
npm install ../my-package
Prior to version 5 of npm, I had no problem as it produce what I expected, ie a folder with the output of what I will publish.
However, using npm 5, it now creates a symlink to my local project as described in the npm documentation:
npm install :
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
How can I use the "old" way to install local project? Or is there a new way to check if my library is correct?
Thank you.
Use npm pack + npm install (as suggested by install-local package)
npm pack <path-to-local-package>
npm install <package-version.tgz>
This will effectively copy your local package to node_modules.
Note that this will package only production relevant files (those listed in the files section of your package.json). So, you can install it in a test app under the package own directory. Something like this:
my-package
package.json
test
test-app
package.json
node_modules
my-package
Assuming that test dir is not included in the files in my-package/package.json.
This works the same way with npm 5 and older versions.
I wrote npm-install-offline which allows you to install npm packages from a local repository or folder. By default it copies the folder on install but you can also choose to symlink.
https://www.npmjs.com/package/npm-install-offline
npx npm-install-offline ../some-package
Or
npx npm-install-offline my-npm-package --repo ./my-offline-npm
It also will install the package dependencies which npm does not do with local packages.

Git dependency with npm keeping .git files

I am using a private GIT repository as dependency in npm:
"name": "git+ssh://git#git.domain.com:user/repo.git"
This is working and clones the repository inside node_modules when I do npm install.
The matter is it deletes .git folder and .gitignore file. I want to keep those file (to do commits later) ¿How to keep those files?
It's better to use npm link ../path-to-local-git after you git clone your dependency repo.
git clone <repo>
cd PROJECT
npm link ../<repo>
and you see the build process run.
It sounds like you would be better served keeping a local checkout of the project and specifying the dependency with a local path.
cd ..
git clone ssh://git#git.domain.com:user/repo.git
cd repo; npm install
cd ../PROJECT
npm i --save ../repo
That way you can make changes and commit them back.
npm treats the contents of node_modules as private, so you should not expect to be able to get into the node_modules directory and do anything useful. If you want to maintain a git checkout of a project which is a dependency, then do that, but don't combine it with the dependency-management that npm does.
Also check out npm link if the dependency is itself a npm-compatible package. https://docs.npmjs.com/cli/install