Jquery not working in angular 6 Error: ENOENT: no such file or directory, open '…\\node_modules\\jquery\\dist\\jquery.min.js'

走远了吗. 提交于 2019-12-02 18:03:18

Once again the angular team makes things harder for not real reason =(

I had this problem and after Googling around fruitlessly I wondered if they had changed relative paths somehow as well as renaming angular-cli.json.

A little further up in the file I found the line:

Yeah, just change:

"../node_modules/jquery/dist/jquery.js",
"../node_modules/tether/dist/js/tether.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"

to

"./node_modules/jquery/dist/jquery.js",
"./node_modules/tether/dist/js/tether.js",
"./node_modules/bootstrap/dist/js/bootstrap.js"

For Angular 6 the path is as follows:

from:

"../node_modules/jquery/dist/jquery.js",
"../node_modules/tether/dist/js/tether.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"

to

"./node_modules/jquery/dist/jquery.js",
"./node_modules/tether/dist/js/tether.js",
"./node_modules/bootstrap/dist/js/bootstrap.js"

Install @ngBootstrap with this npm install --save @ng-bootstrap/ng-bootstrap and npm install jquery --save or npm install @types/jquery --save after the installation go to angular.json and locate where

"styles": [
              {
                "input": "styles.css"
              }
            ],
            "scripts": []

and change it to this :

"styles": [
    "styles.css",
    "./node_modules/bootstrap/dist/css/bootstrap.min.css"
  ],
  "scripts": [
    "./node_modules/jquery/dist/jquery.min.js",
    "./node_modules/bootstrap/dist/js/bootstrap.min.js"
  ]

and ng server --open to run your project

In Angular 6 u don't need to write:

"../node_modules/jquery/dist/jquery.js",
"../node_modules/tether/dist/js/tether.js",
"../node_modules/bootstrap/dist/js/bootstrap.js"

You can write with ./ like on the top this will works too, but the easiest way in angular.json file:

"node_modules/jquery/dist/jquery.js",
"node_modules/tether/dist/js/tether.js",
"node_modules/bootstrap/dist/js/bootstrap.js"

In my opinion it's better and clear

In your file 'tsconfig.json', you make maybe

{
  "compileOnSave": false,
  "compilerOptions": {
  "baseUrl": "./",
  "outDir": "./dist/out-tsc",
  "sourceMap": true,
  "declaration": false,
  "moduleResolution": "node",
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "target": "es5",
  "typeRoots": [
  "node_modules/@types"
  ],
  "lib": [
    "es2017",
    "dom"
  ]
 },
"include": ["node_modules/angular-bootstrap-md/**/*.ts",  "src/**/*.ts"], 
}

You can replace

"include": ["node_modules/angular-bootstrap-md/**/*.ts",  "src/**/*.ts"],

to

"include": ["src/**/*.ts","node_modules/angular-bootstrap-md/**/*.ts" ],

Moreover, in your file 'angular.json', I delete '../' in front of node_modules in "styles" and "scripts".

And I copied lines of the "scripts" in the "scripts" in the category "test"

"test": {
     ...
     "styles": [
       "src/styles.css"
        "node_modules/bootstrap/dist/css/bootstrap.min.css",
        "./assets/css/font-icon.css",
        "node_modules/font-awesome/css/font-awesome.css"
     ],
     "scripts": [
        "node_modules/jquery/dist/jquery.min.js",
        "node_modules/popper.js/dist/umd/popper.min.js",
        "node_modules/bootstrap/dist/js/bootstrap.min.js"
     ],
     ...

Relative path ../ used to go back one directory. So, first check the file path, if its not contain your project root folder you are searching that jquery file somewhere else that even not exist.

If node_modules file and angular.json both in same directory you donot need to use relative path.

In Angular 6, you donot need to use relative path to go inside node_modules from angular.json(../ or even like ./).

Techtiger255 Code Mode

I had a similar issue...

The Jquery plugin didn't have an npm package ready : (
So I decided to put it inside the assets folder...
Evidently that didn't work.
so I did some testing...

Here's what worked for me:

  • Navigate to node_modules/jquery/dist/

  • — and simply insert the plugin file there.

  • Once this is done change your angular.json file
    from:

    "scripts": [ "node_modules/jquery/dist/jquery.min.js", "../assets/libraries/okshadow.min.js" ]

To:

"scripts": [
      "node_modules/jquery/dist/jquery.min.js",
      "node_modules/jquery/dist/okshadow.min.js"
]

And your all set!

P.S. This is on Angular 7.0 rather than Angular 6

"node_modules/bootstrap/dist/css/bootstrap.min.css" not "../node_modules/bootstrap/dist/css/bootstrap.min.css"

We don't use a relative path.

Paths in angular-cli.json are relative to the project root (src/ normally). For instance, you have the src/styles.css file, but in angular-cli.json it's listed only as styles.css.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!