简体   繁体   English

import _ from 'lodash-es/lodash.js' 曾经可以工作。 现在它没有,但我没有改变任何东西

[英]import _ from 'lodash-es/lodash.js' used to work. Now it doesn't but I didn't change anything

I made a small test project to make sure I know how to use lodash in a browser environment:我做了一个小测试项目,以确保我知道如何在浏览器环境中使用 lodash:

/* File in directory: /ESModule/App.js */
// error message
import _ from 'lodash-es/lodash.js';
// relative path works
// import _ from '../node_modules/lodash-es/lodash.js';
const first = [1];
const second = [2];
const combined = _.union(first, second);

document.querySelector(`p`).textContent = combined;

It worked fine at the time, but now that I'm revisiting it a month later, I get this error: "Uncaught TypeError: Error resolving module specifier: lodash-es/union".当时它运行良好,但现在我在一个月后重新访问它,我收到此错误:“未捕获的类型错误:解析模块说明符时出错:lodash-es/union”。 I didn't touch the project at all since then, and the only change to my environment is that I updated NodeJS.从那以后我根本没有接触过这个项目,我环境的唯一变化是我更新了 NodeJS。

Not sure if this helps, but I have lodash-es in my package.json dependencies (installed locally), and I'm on Windows 10. I also have Webpack installed so I could test minifying the project in a separate directory, but it bothers me that I don't know why writing my import paths a certain way suddenly stopped working. Not sure if this helps, but I have lodash-es in my package.json dependencies (installed locally), and I'm on Windows 10. I also have Webpack installed so I could test minifying the project in a separate directory, but it困扰我的是我不知道为什么以某种方式编写导入路径突然停止工作。 I recently noticed that I'm having the same problem on other projects that are more complex, and I'd rather not have to use relative paths like this going forward:我最近注意到我在其他更复杂的项目上遇到了同样的问题,我宁愿不必使用这样的相对路径:

import someModule from '../../../../node_modules/package-name/file.js';

When this should work fine什么时候应该可以正常工作

import someModule from 'package-name/file.js';

Any ideas on what could be causing this issue?关于可能导致此问题的任何想法?

EDIT: I just realized that I also tested Lodash with individual imports编辑:我刚刚意识到我还使用单独的导入测试了 Lodash

import union from 'lodash-es/union';
const first = [1];
const second = [2];
const combined = union(first, second);
document.querySelector(`p`).textContent = combined;

Somehow even this worked in the past, even without ending the path with.js.不知何故,即使这在过去也有效,即使没有用 .js 结束路径。 These ways all give me the same type of error:这些方式都给了我相同类型的错误:

import _ from 'lodash-es';
import _ from 'lodash-es/lodash';
import _ from 'lodash-es/lodash.js';
import union from 'lodash-es/union';
import union from 'lodash-es/union.js';
import {union} from 'lodash-es';

Uncaught TypeError: Error resolving module specifier: [specified path here]未捕获的 TypeError:解析模块说明符时出错:[此处指定路径]

import _ from '/node_modules/lodash-es/lodash.js';
import union from '../node_modules/lodash-es/union.js';
import union from '/node_modules/lodash-es/union.js';

Only a relative path or specific absolute path works now, so at least I know I probably have it installed correctly.现在只有相对路径或特定的绝对路径有效,所以至少我知道我可能正确安装了它。 As I mentioned above, all of the code used to work, but without changing it or adding to or removing from node_modules, now it doesn't.正如我上面提到的,所有代码过去都可以工作,但没有更改它或添加到 node_modules 或从 node_modules 中删除,现在它不行了。

One thing I forgot to mention was that I'm using the Live Server extension, and usually it defaults to port 5500 when I start it up, but lately it's been having trouble finding available ports and uses numbers like 5522 and 5909. Lately I've also been having problems downloading various files types for example I tried downloading a new NodeJS install file and I got this error:我忘记提到的一件事是我正在使用 Live Server 扩展,通常在我启动它时它默认为端口 5500,但最近它在查找可用端口时遇到了麻烦,并且使用了 5522 和 5909 之类的数字。最近我下载各种文件类型时也遇到问题,例如我尝试下载新的 NodeJS 安装文件,但出现此错误:

C:/Users/DANIEL~1/AppData/Local/Temp/D1JF2y30.msi.part could not be saved, because the source file could not be read.

Maybe there's a problem with my machine that's causing a problem with my browsers and LS?也许我的机器有问题导致我的浏览器和 LS 出现问题?

EDIT: I tried using lodash with NodeJS instead of testing in a browser with Live Server, and it works fine using just the package path const union = require('lodash/union');编辑:我尝试将 lodash 与 NodeJS 一起使用,而不是在具有 Live Server 的浏览器中进行测试,并且仅使用 package 路径const union = require('lodash/union');工作

EDIT: I just noticed that if I try to minify with Webpack, I can use the package path without any problem, and I don't need to end with.js.编辑:我刚刚注意到,如果我尝试使用 Webpack 进行缩小,我可以毫无问题地使用 package 路径,并且我不需要以.js 结尾。 Maybe I wasn't clear before, but I was having issues with package paths without using Webpack.也许我以前不清楚,但我在不使用 Webpack 的情况下遇到了 package 路径的问题。 Was it weird that using import _ from 'lodash-es/lodash.js';使用import _ from 'lodash-es/lodash.js';很奇怪吗? without Webpack even worked in the first place?没有 Webpack 甚至可以先工作?

Since you're importing the whole library you can just do:由于您要导入整个库,您可以这样做:

import _ from 'lodash-es';

The slash notation is only needed when specifying a specific function to import.仅在指定要导入的特定 function 时才需要斜杠表示法。 (This way is recommended since importing _ brings in hundreds of functions.) (推荐这种方式,因为导入_会带来数百个函数。)

import union 'lodash-es/union';

The best way is to only install one method and import that.最好的方法是只安装一种方法并将其导入。

npm install --save lodash.union

import union from 'lodash-es.union';

See this thread for more details.有关更多详细信息,请参阅此线程

Import whole lodash as _:将整个 lodash 导入为 _:

TypeScript/Angular:打字稿/角度:

import * as _ from 'lodash-es';

...
const combined = _.union(first, second);

ES: ES:

import _ from 'lodash-es';

Import method:导入方式:

import {union} from 'lodash-es';

...
const combined = union(first, second);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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