简体   繁体   中英

Angular 2 how to import libraries in node_modules?

I've been trying to import node_modules libraries into my app but the console keeps on returning

GET http://localhost:4200/node_modules/systemjs/dist/system.src.js 

not found. Does any know how to fix this?

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>PrcApplication</title>

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src="../node_modules/systemjs/dist/system.src.js"></script>
</head>
<body>
  <app-root>Loading...</app-root>
</body>

</html>

在HTML标头下添加以下内容,然后尝试:

  <base href="/">

Adding a script tag will cause an ajax call to that path, which is not what you want because in production, your node_modules folder is not available, and there is only dist folder to be available.

In these cases, you'd need to import that particular file inside one of your own js/ts files so the file would be imported and packed together with your src files.

I don't know what you're using for bundling or rather even you have bundling or not, but you can import it in your app.module.ts or somewhere similar.

Normally there is a file called vendor.ts or pollyfill.ts which is designed for these things.

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