Let me try to explain...
I have an utils.js file which does not work:
import dayjs from 'dayjs'; //https://github.com/iamkun/dayjs
exports.utils = (function() {
someDateFunction() {
...some dayjs function calls ....
}
})();
(As stated by an answering person thta it needs to be module.exports - well it does not matter. I have other source files where it works either way - with or without prenoted module..)
When declaring the first line with the import statement vue-cli seems to properly compile. no error is shown. Running in the browser shows:
Uncaught ReferenceError: exports is not defined
at eval (utils.js?2f14:3)
at Module../src/assets/js/utils.js (app.js:1541)
at __webpack_require__ (app.js:849)
at fn (app.js:151)
...
It do include/import this utils.jsfile by noting
const { utils } = require("@/assets/js/utils.js");
Why is it not allowed to import in the file?
If you need informations about versions I run let me know and also let me know what commands will show the demanded information (running on macos).
I already searched the web. the problem seems to be known in the past. There is some clues solving it with typescript or vue js.
Nothing works for me. I must not be the first one stumbling over issues like that, am I? Any help appreciated.
References:
https://stackoverflow.com/a/55334703/845117
My dependencies in package.json
"dependencies": {
"axios": "^0.21.1",
"bootstrap": "^4.5.3",
"bootstrap-vue": "^2.18.0",
"core-js": "^3.6.5",
"cropperjs": "^1.5.9",
"dayjs": "^1.9.6",
"vue": "^2.6.12"
},
-
delete node_modules and install againtuhin47– tuhin472021年02月21日 07:03:42 +00:00Commented Feb 21, 2021 at 7:03
-
I can't reproduce this error in a default Vue CLI scaffolded project, using the code in question. Can you link to a GitHub repo that reproduces the problem?tony19– tony192021年02月21日 08:14:14 +00:00Commented Feb 21, 2021 at 8:14
-
@tuhin47 your proposal did not work. I also fully set up the project with vue-cli.Dirk Schumacher– Dirk Schumacher2021年02月21日 18:55:46 +00:00Commented Feb 21, 2021 at 18:55
2 Answers 2
One solution I just found:
instead of
import dayjs from 'dayjs';
I use
const dayjs = require('dayjs');
That works. I figured it by by reading https://www.educba.com/require-vs-import/
I cannot really argue why it works the one and not the other way.
(Since I am not a native speaker I just understood 15% of the article. And I have not yet tried tuhin47's proposal)
Comments
It says exports does not exist because it should be module.exports = <your function definition>