使用带有 angular-cli 的 Sequelize 找不到模块错误
Module not found error using Sequelize with angular-cli
我有一个使用 angular-cli (v 1.3.2) 设置的 angular 2/4 应用程序。我有一个 MAMP mysql 服务器 运行,我可以在 Sequel Pro 中看到它。
为了使用 sequelize 进行连接,我遵循了 getting started docs. (And I installed @types/sequelize。)但我得到 mysql 和 postgres 方言的 Module not found: Error: Can't resolve...
。
这是我的代码:
import { Injectable } from '@angular/core';
import * as Sequelize from 'sequelize';
@Injectable()
export class DataService{
public sequelize: Sequelize.Sequelize;
constructor(){
this.sequelize = new Sequelize('local-db', 'username', 'password', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
this.init();
}
public init(){
this.sequelize.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.log('Unable to connect to the database:');
console.error(err);
});
};
}
也许 webpack 可能会阻塞 3rd 方库的一些延迟加载资源(只是从 this issue 猜测)?因此,我在 .angular-cli.json:
中添加了 sequelize 作为外部库
"scripts": [
"../node_modules/sequelize/lib/sequelize.js"
]
但我遇到了同样的错误:
ERROR in ./node_modules/sequelize/lib/dialects/mssql/query.js
Module not found: Error: Can't resolve 'tedious' in '[MYAPPROOT]/node_modules/sequelize/lib/dialects/mssql'
resolve 'tedious' in '[MYAPPROOT]/node_modules/sequelize/lib/dialects/mssql'
Parsed request is a module
using description file: [MYAPPROOT]/node_modules/sequelize/package.json (relative path: ./lib/dialects/mssql)
Field 'browser' doesn't contain a valid alias configuration
after using description file: [MYAPPROOT]/node_modules/sequelize/package.json (relative path: ./lib/dialects/mssql)
resolve as module
[MYAPPROOT]/node_modules/sequelize/lib/dialects/mssql/node_modules doesn't exist or is not a directory
[MYAPPROOT]/node_modules/sequelize/lib/dialects/node_modules doesn't exist or is not a directory
[MYAPPROOT]/node_modules/sequelize/lib/node_modules doesn't exist or is not a directory
[MYAPPROOT]/node_modules/node_modules doesn't exist or is not a directory
yada
yada
yada
我在sequelize连接中看到-manager.js,查找模块可能有问题:
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error('Please install \'' + (sequelize.config.dialectModulePath || 'pg') + '\' module manually');
}
但我已经按照入门说明安装了 mysql2。
那么缺少什么?任何对这个谜团的帮助将不胜感激!
"getting started" 文档没有明确说明,但它们指的是 node.js 应用程序,而不是 angular 应用程序。您几乎肯定不想从 angular 应用程序连接到 SQL。您应该创建一个连接到您的数据库的 "backend" node.js 应用程序,并使用某种 API.
从 angular 与该应用程序通信
从技术上讲,可以从 angular 访问 mysql,但这需要:
- 将您的登录凭据放入 angular 应用程序。任何人
使用您的应用程序的人将能够看到它们
- 当你为连接输入 "localhost" 时,这是你的本地主机
浏览器 这意味着它只能在您的浏览器和数据库
运行 在同一台机器上。
我有一个使用 angular-cli (v 1.3.2) 设置的 angular 2/4 应用程序。我有一个 MAMP mysql 服务器 运行,我可以在 Sequel Pro 中看到它。
为了使用 sequelize 进行连接,我遵循了 getting started docs. (And I installed @types/sequelize。)但我得到 mysql 和 postgres 方言的 Module not found: Error: Can't resolve...
。
这是我的代码:
import { Injectable } from '@angular/core';
import * as Sequelize from 'sequelize';
@Injectable()
export class DataService{
public sequelize: Sequelize.Sequelize;
constructor(){
this.sequelize = new Sequelize('local-db', 'username', 'password', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
this.init();
}
public init(){
this.sequelize.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.log('Unable to connect to the database:');
console.error(err);
});
};
}
也许 webpack 可能会阻塞 3rd 方库的一些延迟加载资源(只是从 this issue 猜测)?因此,我在 .angular-cli.json:
中添加了 sequelize 作为外部库"scripts": [
"../node_modules/sequelize/lib/sequelize.js"
]
但我遇到了同样的错误:
ERROR in ./node_modules/sequelize/lib/dialects/mssql/query.js
Module not found: Error: Can't resolve 'tedious' in '[MYAPPROOT]/node_modules/sequelize/lib/dialects/mssql'
resolve 'tedious' in '[MYAPPROOT]/node_modules/sequelize/lib/dialects/mssql'
Parsed request is a module
using description file: [MYAPPROOT]/node_modules/sequelize/package.json (relative path: ./lib/dialects/mssql)
Field 'browser' doesn't contain a valid alias configuration
after using description file: [MYAPPROOT]/node_modules/sequelize/package.json (relative path: ./lib/dialects/mssql)
resolve as module
[MYAPPROOT]/node_modules/sequelize/lib/dialects/mssql/node_modules doesn't exist or is not a directory
[MYAPPROOT]/node_modules/sequelize/lib/dialects/node_modules doesn't exist or is not a directory
[MYAPPROOT]/node_modules/sequelize/lib/node_modules doesn't exist or is not a directory
[MYAPPROOT]/node_modules/node_modules doesn't exist or is not a directory
yada
yada
yada
我在sequelize连接中看到-manager.js,查找模块可能有问题:
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error('Please install \'' + (sequelize.config.dialectModulePath || 'pg') + '\' module manually');
}
但我已经按照入门说明安装了 mysql2。
那么缺少什么?任何对这个谜团的帮助将不胜感激!
"getting started" 文档没有明确说明,但它们指的是 node.js 应用程序,而不是 angular 应用程序。您几乎肯定不想从 angular 应用程序连接到 SQL。您应该创建一个连接到您的数据库的 "backend" node.js 应用程序,并使用某种 API.
从 angular 与该应用程序通信从技术上讲,可以从 angular 访问 mysql,但这需要:
- 将您的登录凭据放入 angular 应用程序。任何人 使用您的应用程序的人将能够看到它们
- 当你为连接输入 "localhost" 时,这是你的本地主机 浏览器 这意味着它只能在您的浏览器和数据库 运行 在同一台机器上。