简体   繁体   中英

Npm module not found

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.json file 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM