Can't resolve a local npm module made by npm pack - node.js

I created a local module using npm pack, which created a package: "org-header-0.0.3.tgz"
In a React project, I added org-header to its package.json:
"dependencies": {
"org-header": "file:[path-to-org-header]/org-header-0.0.3.tgz",
...
}
It installs fine. However, when I try to use it:
import Header from 'org-header';
...
However when I do npm run start:
Failed to compile.
./src/components/Layout.js
Module not found: Can't resolve 'org-header' in '/Users/imyjimmy/***/src/components'
Could it be because:
I did not structure the org-header project correctly
Webpack is doing something weird
Both?

I also encounter the same issue the other day. Just post my solution here if it helps.
So I'm testing a local dependency (project B) update in my main project (project A) through
npm pack --> generate the tgz file
refer the tgz file in the project A's package.json as above question shows
run npm install in project A to unpack the package -- installation works fine
run into below error when npm develop
Generating development JavaScript bundle failed
Can't resolve 'project B' in '/Users/xxx#xxx.com/projectA/src/pages'
What this error indicates is that the package Project A does not exist.
This is weird because when I ran npm list project B I actually got the confirmation that it exists.
The solution is to explicitly install project B again e.g.npm install project B , instead of running npm install after you change the package.json in step 3.
I don't know if it's webpack got confused by the 2 packages or somewhat, but the solution solved my problem .

Related

Node is not getting installed and it is showing error

I was installing node by using the command on cmd npm install and I got following message
enter image description here
Please help me that how I can solve this issue.
npm stands for node package manager which is used to install external packages or npm modules or libraries hosted on NPM. To get started with a Node JS Project you will need index.js and hit npm init, this will initialise your Node JS Project with auto genreated package.json.
Majorly Node JS, React JS or Angular contains following files:
index.js/server.js(Entry File)
package.json (modules/packages/libraries used in project and script can be written to run your project in different envs)
node_modules (which is auto generated and contains all installed modules listed in package.json)
According to your error message you don't have a package.json file so you need to initialize npm before installing node in your directory.
Initialize it with npm init.
Npm documentation
Step 1- First install Node from https://nodejs.org/en/download/.
Step 2- Now go to your project file cd project.
Step 3- Type the command npm init in console.
Step 5- Now you can see package.json file in your project.
Finish- Now you can install any package by using "npm install packageName" command.

How to use locally built NPM package in project

I'm new to NPM (obviously) and I have forked a NPM module to test out some custom modifications to it - let's call that project dependsOnMe. Now i want to use DependsOnMe in my CustomApp project. I can't figure out how to do this.
In my DependsOnMe project I did a npm install -g . (whatever that does, it's now unclear to me), but when I go to my CustomApp project and add a dependency for DependsOnMe into my package.json NPM tells me it cant find DependsOnMe in the NPM remote repo (no kidding...).
Since I installed DependsOnMe globally I thought perhaps I don't need to declare a dependency in MyCustomApp, but when I try to include DependsOnMe in one of my project's .js files I get the error that DependsOnMe cannot be found.
This must be simple, what am I missing?

Why doesn't npm install correctly build dependency from a fork of a package on npm?

I have a project that uses an older version of react-bootstrap
in my package.json I had
"react-boostrap": "^0.13.3"
And there is a bug in there that is causing an issue, the fix for it is very simple so I forked the repo, rolled back to the commit for v0.13.3 doing:
git reset --hard <commit-hash>
made my 1 line fix, then force pushed back to my fork.
Then I updated my package.json in the main project to point to my fork:
"react-boostrap": "mygithubrepo/react-bootstrap"
I deleted the node_modules directory and ran sudo npm cache clear to make sure everything was nice and fresh. Then I ran npm install which did pull down my version of react-bootstrap from the repo.
However, the version it now pulls down isn't in the same format in the node_modules folder. Essentially, it doesn't seem as if it's built the project.
If I use:
"react-boostrap": "^0.13.3"
then the node_modules folder contains the built code like so:
+node_modules
--+react_bootstrap
----+utils
-----Acordion.js
etc.....
But when I point it to my forked version of the repo:
"react-boostrap": "mygithubrepo/react-bootstrap"
Then I get just the source repo structure in node modules (and it doesn't seem to be built)
+node_modules
--+react_bootstrap
----+docs
----+ie8
----+src
-------+utils
--------Acordion.jsx
........etc.....
----+tools
etc.....
Now when I reference react bootstrap using require('react-bootstrap') then it doesn't work (because the files haven't been built).
So what is different between me pointing to the file on npmjs rather than on github? There isn't any prepublish/publish/postpublish script in react-bootstrap package.json so I'm not sure if they manually built it before pushing to npmjs.
Basically, I need to get my forked version automatically built when I run npm install on my main project. Any ideas?
Edit 1
The errors I get running npm install on the main project repo after updating my fork of react-bootstrap as per Jonathan Muller's answer below:
>> Local Npm module "grunt-contrib-uglify" not found. Is it installed?
>> Local Npm module "grunt-amd-wrap" not found. Is it installed?
>> Local Npm module "grunt-react" not found. Is it installed?
>> Local Npm module "grunt-contrib-clean" not found. Is it installed?
>> Local Npm module "grunt-contrib-watch" not found. Is it installed?
>> Local Npm module "grunt-contrib-copy" not found. Is it installed?
>> Local Npm module "grunt-browserify" not found. Is it installed?
>> Local Npm module "grunt-contrib-requirejs" not found. Is it installed?
Warning: Task "clean:amd" not found. Use --force to continue.
Aborted due to warnings.
In your package.json, add the following to build it when you npm install it:
{
"scripts": {
"postinstall": "grunt build"
}
}
This way the package will be built after install
Then to get it working with require, add the following to the package.json:
{
"main": "amd/react-bootstrap.js"
}
So the correct file can be found when you "require" it. Otherwise it search for index.js

using a coffeescript node module in a nodejs project

I am playing with a node module called filings
when I first used it, I did a git clone to get that project into a local directory, where I ran npm install and then grunt to build all of the coffeescripts into JS and get dependencies installed.
That works fine and I end up with a /lib directory with 13 files in it.
Now, i'm trying to use that module within a separate node project, so I installed it with npm install filings and a node_modules/filings/lib directory was created, but it only has 6 files in it, and a bunch of functionality is missing.
I've never installed a coffeescript node module into a regular node project before, is there something I can do to get it to properly build within my project?
This is a problem with the package on npmjs.org: If we clone a module from the repository, install it, and make package with npm pack then we get a very different contents of the package to install than the package manager on the site:
http://registry.npmjs.org/filings/-/filings-0.2.0.tgz
coffee-script require (generally spoken)
you can require coffee-script files from a javascript-file if you use coffee-script/register before.
therefore you need to install coffee-script locally:
$ npm install coffee-script --save
and use it in your code:
require('coffee-script/register');
require('youre-coffee-script-module');
using the filings-module
basic setup of filings
the module itself has its main-script set to ./lib/filings.js
the lib-folder is compiled by the grunt-task publish
the publish-Task itself is triggered via the prepublish-script. so anytime the package-auther publishes his package via npm publish the lib-sources are build, an afterwards the compiled package is published.
in the .npmignore file all the source and testfiles are ignore, and therefore not published to npm!
how to fix your problem
i just see 2 more or less clean possibilities:
open an issue, and ask the package-auther to publish a new version to npm
create a fork, and use the fork
a) fork the repo
b1) add a postinstall-Property which runs the grunt-default-task grunt:
"scripts": {
"postinstall": "grunt"
}
or b2) create a index.js file where you require the coffee-sources like i pointed out above
c) install the package from your repo: npm install git+ssh://git#github.com:YOURGITHUBUSERNAME/filings.git
d) maybe create a pull-request if you do version b2

How generate nodejs express dependencies package.json

As I started to develop my first nodejs express application, I added many packages with npm.
I would like to know if there is a way to generate a package.json file containing all the current dependencies or the list of the current packages under the nodes_modules directory.
Just run npm init in the same directory as your project.
You'll be asked a list of questions (name, version, description, etc.) and once complete, it will generate a package.json file with all of the dependencies as currently installed in the node_modules directory.
Run npm list to see what you have installed. Run npm shrinkwrap to build a npm-shrinkwrap.json file, which you can use as a starting reference to build a proper package.json. My workflow is always to update package.json and then run npm install. I never run npm install foo to get some package because it creates risk of forgetting to add it to package.json and then having your application fail to start on deployment.
Updated to add: These days I do run npm install --save foo or npm install --save-dev foo since I have now decided the ~0.4.3 version numbers it adds to package.json are better than my former preference for 0.4.x since the ~ gives you a more precise minimum version number.

Resources