简体   繁体   English

Vuejs - 无法解析“app/src”中的“时刻”| 在项目克隆和 npm install 之后

[英]Vuejs - Can't resolve 'moment' in 'app/src' | After a project clone and npm install

I cloned an existant Vuejs project and made a "npm install".我克隆了一个现有的 Vuejs 项目并进行了“npm 安装”。 Everything installed well except "moment" and maybe "vue-moment".除了“moment”和“vue-moment”之外,一切都安装得很好。

i've got this error :我有这个错误:

Failed to compile.
./src/main.js
Module not found: Error: Can't resolve 'moment' in '/app/src'

so in installed them with "npm install --save moment vue-moment"所以在安装它们时使用“npm install --save moment vue-moment”

And still got the error.并且仍然得到错误。

here some code of my main.js file :这是我的 main.js 文件的一些代码:

import moment from 'moment'
import VueMoment from 'vue-moment'

// Vue use moment.js
Vue.use(VueMoment, {
  moment
});
moment.locale('fr');

the packages are installed and present in the node_modules folder and are present also in the package.log.json and package.json.软件包已安装并存在于 node_modules 文件夹中,并且也存在于 package.log.json 和 package.json 中。

The weird things is, when other people clones this project, they don't have the problem.奇怪的是,当其他人克隆这个项目时,他们没有问题。 only me...只有我...

One (maybe) pist, is when i put my mouse hover the 'moment' (of the import), i see this : .../node_modules/moment/moment like there is 2 moment folders, but it looks normal in the node_modules folder.一个(可能)pist,是当我将鼠标悬停在(导入的)“时刻”时,我看到:.../node_modules/moment/moment 就像有 2 个时刻文件夹,但它在 node_modules 中看起来很正常文件夹。

Thanks in advance!提前致谢!

EDIT SOLVED :编辑解决:

The problem was because i started the project with "docker-compose up" and not "npm run serve" on the FRONT.问题是因为我在 FRONT 上使用“docker-compose up”而不是“npm run serve”启动了项目。 It started but i had this "moment" problem.它开始了,但我遇到了这个“时刻”问题。 Now it works well with the good console code to start the project ...现在它可以很好地与良好的控制台代码一起启动项目......

You cannot use the import function with moment.js since exports is not defined for that module.您不能在 moment.js 中使用 import 函数,因为没有为该模块定义导出。 So you have to use require.所以你必须使用require。 You can do this :你可以这样做 :

Vue.use(require('vue-moment'));

If you really want to use moment.js using import you can do this as well.如果你真的想通过 import 使用 moment.js,你也可以这样做。

import * as moment from 'moment';

Reference: https://www.npmjs.com/package/vue-moment参考: https : //www.npmjs.com/package/vue-moment

https://momentjs.com/docs/ https://momentjs.com/docs/

Webpack / typescript require works but import doesn't Webpack / typescript 需要工作但导入没有

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

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