Npm module not found - javascript

I'm running an Angular app built with Grunt and using Bower and NPM.
I tried installing my npm module locally. The files are in the main app directory in node_modules folder.
The module docs ask me to load the module with <script type="text/javascript" src="node_modules/moment/moment.js"></script>, but I get 404.
Am I missing something? Do I have to tell Grunt that I want these NPM modules?

Can you provide more information on what your app is built with? If node serves your app, you need to make the directory you link to public. Assuming you're using express, this would look something like this in your app.js file:
app.use('/node_modules', express.static(__dirname + '/node_modules/moment/moment.js'));
Edit:
Or if you just want to make it work, try to load moment.js from CDN like this:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
Link to moment on CDN

Basically, npm is the package manager for all the javaScript frameworks such as Nodejs, angularjs etc. npm should be installed globally in the machine.You can install it from https://nodejs.org/en/ .
Next,you need check for the package.json file in your project.
If there is a package.json already existing in your project folder, then from command line you need to go to your project folder and type npm start.
If package.json file does not exist, then in the command line type npm init,then there will be a package.jsonfile created in your project folder.Then edit the package.json . and add the node packages into the package.json as similar way to this
{
"name": "shoppingkart",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www" //If you have any scripts.
},
"dependencies": {
"mongoose": "^4.9.0", // here you should have all your node_modules listed
"passport": "^0.3.2",
"stripe": "^4.15.1"
}
}
if you are not able to add the dependencies to json file, there is also another way to do it.
just go to your project directory in the command line and type
npm install --save grunt // And you need to do for all the node_modules, by replacing the **grunt**.
Automatically the dependency will be added to your package.json file.

If you installed your npm packages locally then your node_modules folder should found at the root of your project.
If you installed all your packages globally you may not see an npm_modules folder in your project.
To see where your global modules are located you can run
npm list -g

I faced the same issue just install the package globally and save at the end.
Like:
npm install -g <package> --save
Even the above doesn't work then use -f / --force at the end to force install.

Related

Deploying Node Application

I am using NestJS and IIS, and I have deployed my dist folder on server through IIS with the help of IISNode, but when I run it, it gives me an error of 'module not found #nestjs/core' etc, so I installed entire package.json files (node_module) on server, after this it start working fine. But I have a question.
Do we have to keep node_modules folder on the server which is of 250MB+?
Do we have any other alternative by which dist will contain all the required code of node_modules just like an Angular application?
This is not NestJS related, but it is related to NodeJS itself. A typical package.json file looks like this:
{
"name": "nest-typescript-starter",
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
"license": "MIT",
"scripts": {
// ...
},
"dependencies": {
"#nestjs/common": "8.2.3",
// ...
},
"devDependencies": {
"#nestjs/cli": "8.1.7",
// ...
}
}
Everything under dependencies are required at runtime, since they're being used by your application. Anything under devDependencies are only required during development, for various reasons.
It is common to have devDependencies like type modules, testing tools, and others.
While installing a new dependency, you have two options:
# Option 1
npm install <dependency name>
# Option 2
npm install --save-dev <dependency name>
If you provide the --save-dev flag you would install that dependency under devDependencies.
After making your package.json file organized, separating devDependencies from dependencies you can deploy your application properly. During the deployment process, instead of running npm install you can use:
npm install --omit=dev
By doing so, you would only install runtime dependencies to your node_modules.
Finally, you can copy that node_modules to your container (or whatever you're using to deploy) and ship the application.

Is there a method to separate dependencies object from package.json file of an angular project?

Here is the scenario
I have multiple angular applications which belong to the same project. When I needed to upgrade an npm package, I need to change all the package.json files in each application. I tried mono repo, but it does not apply to my project because npm scripts are not the same as all apps.
So I want to separate the node dependencies from applications and keep npm scripts of the package.json files.
Is there a way to separate only the dependencies object from the package.json file? OR any other suggestions?
If I understood well this could not be done with npm but seems like it can be achieved with yarn workspaces:
https://classic.yarnpkg.com/en/docs/workspaces/
Guide for installing yarn:
https://classic.yarnpkg.com/en/docs/install/
Unfortunately npm do not support package.json inheritance. See this link.
On the other side why don't you customize your npm scripts? create alias for every npm command.
e.g.
"test-app1": "ng test app1",
"test-app2": "ng test app2 --code-coverage --no-watch --no-progress --browsers ChromeHeadlessNoSandbox",

Can (and should) I use Lerna without publishing to NPM?

I have an app and I have a storybook. They both live inside the same app at the moment. I have just installed Lerna and played around so I can treat them both as 2 separate packages.
Both packages will be dockerised and when I build one, it should not do anything with the other
my structure so far:
app/
package.json
lerna.json
makefile
packages/
my-app/
all-relevant-packages-for-my-app
package.json
storybook/
all-relevant-packages-for-storybook
package.json
I don't want to publish anything to NPM. I simply want to use these packages separately but I would like to import app components into the storybook
is Lerna the correct thing to use here?
when I run say docker build my-app it should go inside the my-app repo and then build the docker image etc, run tests and deploy my app to S3 (this is all set up already) but now I've moved it all I'm wondering if Lerna is the correct tool as I see lots about publishing to NPM
You don't have to use Lerna in your case. I'd recommend you to use Yarn Workspaces. Let's install yarn and then add
"workspaces": [
"packages/*"
],
section to your main package.json file. From now on, yarn will be handling packages dependencies.
In your case - please enter the package.json of my-app and change its name to e.g. `#app/my-app'. Check the version of my-app in package.json (I will assume its 1.0.0 for now).
Do the same with storybook changing its name to e.g. #app/storybook. Then you can add a dependency in storybook's package.json file:
"dependencies": {
"#app/my-app": "1.0.0"
...
}
Go back to the root directory app and run yarn command. It will resolve all dependencies for all packages. Now you can use my-app inside storybook without the necessity of using Lerna.

How to install npm dependencies inside a sub directory?

I have a package.json in the root: "install": "cd packages/my-package && yarn".
When I run yarn run install all I'm asking is that it goes into this package, installs the node_modules and dependencies and that's it. For some reason no matter what I try (yarn workspaces/lerna/manually etc.) it is installing node_modules in all packages, including the root.
WHY?! Just let me install them separately :(
Anyone knows how to easily fix this?
To move the basefolder of node_modules, you will have to create a .yarnrc file in the same directory as your package.json file. Your .yarnrc file should look like
--modules-folder packages/my-packages
The resulting output will put all of your packages after my-packages
/packages/my-packages/WHERE_YOUR_PACKAGES_GO

npm start and install are not working

I tried to run npm install and npm start but both does not work. I reinstalled the nodejs but still the same. How to resolve it?
If you're trying to install the dependencies of an existing project (which the context implies), you need to run the command npm install in the directory where the package.json file of the project is.
If you've created a new and empty project folder, you need to first run npm init to create a package.json file which is used to manage the dependencies of your project.
Are you sure you're in the head directory? For example, if your project is in:
$ Documents/Projects/JS-Projects/Work/Client-Management-App/
Then that's the folder you need to be in: the Client-Management-App/ directory, because that's where the package.json file and the /node_modules/ folder will be created.
Also, have you run npm init? That might be another reason why.

Categories

Resources