我试图在laravel项目中使用moment.js,而不使用vue,但仍通过npm导入软件包。
我的app.js文件:
0 info it worked if it ends with ok
1 verbose cli [ '/Users/x/.nvm/versions/node/v10.0.0/bin/node',
1 verbose cli '/Users/x/.nvm/versions/node/v10.0.0/bin/npm',
1 verbose cli 'run-script',
1 verbose cli 'build' ]
2 info using npm@5.6.0
3 info using node@v10.0.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle lulo@0.1.0~prebuild: lulo@0.1.0
6 info lifecycle lulo@0.1.0~build: lulo@0.1.0
7 verbose lifecycle lulo@0.1.0~build: unsafe-perm in lifecycle true
8 verbose lifecycle lulo@0.1.0~build: PATH: /Users/x/.nvm/versions/node/v10.0.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/x/Lulo/lulo/node_modules/.bin:/Users/x/.nvm/versions/node/v10.0.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
9 verbose lifecycle lulo@0.1.0~build: CWD: /Users/x/Lulo/lulo
10 silly lifecycle lulo@0.1.0~build: Args: [ '-c', 'react-scripts-ts build' ]
11 silly lifecycle lulo@0.1.0~build: Returned: code: 1 signal: null
12 info lifecycle lulo@0.1.0~build: Failed to exec build script
13 verbose stack Error: lulo@0.1.0 build: `react-scripts-ts build`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (/Users/x/.nvm/versions/node/v10.0.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack at EventEmitter.emit (events.js:182:13)
13 verbose stack at ChildProcess.<anonymous> (/Users/x/.nvm/versions/node/v10.0.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:182:13)
13 verbose stack at maybeClose (internal/child_process.js:947:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:236:5)
14 verbose pkgid lulo@0.1.0
15 verbose cwd /Users/luisvalencia/Lulo/lulo
16 verbose Darwin 18.0.0
17 verbose argv "/Users/x/.nvm/versions/node/v10.0.0/bin/node" "/Users/x/.nvm/versions/node/v10.0.0/bin/npm" "run-script" "build"
18 verbose node v10.0.0
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error lulo@0.1.0 build: `react-scripts-ts build`
22 error Exit status 1
23 error Failed at the lulo@0.1.0 build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
我的webpack.pix.js文件中的看起来像这样:
require('moment/moment.js');
require('./bootstrap');
require('bootstrap-select');
然后在我的custom.js文件中,我得到了这个信息:
const mix = require('laravel-mix');
mix.setResourceRoot('laravel/public');
mix.js('resources/js/app.js', 'public/js')
.combine([
'resources/theme/dist/js/app.js',
'resources/theme/dist/js/app.init.js',
'resources/theme/dist/js/sidebarmenu.js',
'resources/theme/dist/js/waves.js',
'resources/theme/assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js',
'resources/theme/assets/extra-libs/sparkline/sparkline.js',
'resources/theme/dist/js/custom.js',
'resources/js/custom.js'
], 'public/js/nice-admin.js')
.copyDirectory('resources/theme/assets/images/', 'public/assets/images')
.copyDirectory('resources/theme/dist/css/icons/material-design-iconic-font/fonts', 'public/fonts')
.copy('node_modules/bootstrap-daterangepicker/daterangepicker.css', 'public/css/daterangepicker.css')
.copy('node_modules/bootstrap-select/dist/css/bootstrap-select.min.css', 'public/css/bootstrap-select.min.css')
.sass('resources/sass/app.scss', 'public/css', {
implementation: require('node-sass')
}).options({processCssUrls: false})
我遇到以下错误:
$(function() {
"use strict";
let date = moment().format();
console.log(date);
});
我已确认该时刻在我编译的app.js文件中。那么为什么我的custom.js文件看不到它,我该怎么做才能解决它?
答案 0 :(得分:2)
目前,您没有为任何事物分配moment
。根据{{3}},您应将其分配给要使用的变量:
var moment = require('moment');
moment()。format();
但是,如果您想在app.js
文件范围之外使用它,则将无法使用。一种方法是将其添加到window
对象中:
window.moment = require('moment');